Why would the company owners share the profits with thousands of people when they could keep profits to themselves? The reason is that at some point every company needs to "raise money". For eg: If suppose I own a land and I don’t have enough money to build house. I am planning to build the house and sell it with profit. To do this, I can either borrow it from somebody or raise it by selling part of my company, which is known as issuing stock. This is IPO.
Generally issuing stocks is advantageous for the company because it does not require the company to pay back the money or make interest payments along the way. A company can also raise more capital than it could borrow. On the other hand stock holders earn a lot if a company is successful, but they also stand to lose their entire investment if the company isn't successful. All that the shareholders get in return for their money is the hope that their shares will someday be worth more than what they paid for them.