And in many non-software inventions, even if you build the product and market it, the secret is not obvious: that is to say, the means of producing that product.
For instance, you might come up with, oh, a high yield method for producing some valuable chemical and keep it secret while just selling the chemical. The ability to obtain the patent encourages the disclosure of the recipe for that high yield method, while ensuring that you get compensated by licensing. This protection lasts for some time, and then the knowledge becomes patent free. In the alternative scenario, the secret process might just die with the company. Or, someone else figures it out not long after, does better marketing, and eats the original inventor's lunch. So there is a wager involved. The certainty of so many years of protection, versus the uncertainty of someone figuring out the secret.