The very first corporate charters were created in Britain. And it was in the sixteenth century. But these were public corporation owned by the government, like the postal service. Privately owned corporations started in the early 19th century in the USA, UK and Western Europe. It happened because the governments of those countries started allowing anyone to create corporations.
For any corporation to function and do good business, it needs to get money. Initially one or more people come together and contribute a primary investment to get the company started. These entrepreneurs may invest some of their own money. In case they don't have enough, they will need to persuade other people to contribute. People such as venture capital investors or banks can invest in the company. There are two ways of doing this, by issuing bonds, which are basically a way of selling debt or taking out a loan. Other option is to issue stock, that is, shares in the ownership of the company.
Long ago stock owners realized that it would be convenient if there were a central place they could go to trade stock with one another, and the public stock exchange was born. Eventually, today's stock markets grew out of these public places. A corporation is generally entitled to create as many shares as it feels. Each share is actually a small piece of ownership in the company. The more shares you own, the more of the company you own, and the more control you have over the company's operations. Companies sometimes issue different classes of shares, which have different privileges associated with them. So a corporation creates some shares, and sells them to an investor for an agreed upon price. The corporation now has money to run their business. In return, the investor has a degree of ownership in the corporation, and can exercise some control over it. The corporation can continue to issue new shares, as long as it can persuade people to buy them. If the company makes a profit, it may decide to reinvest the money back into the business or use some of it to pay dividends to the shareholders.
Most companies that go public have been around for some time. Going public gives the company an opportunity for a potentially huge capital infusion, since millions of investors can now easily purchase shares. But it also exposes the corporation to stricter regulatory control by government regulators. When a corporation decides to go public, after filing the necessary paperwork with the government and with the exchange it has chosen, it makes an initial public offering (IPO). The company will decide how many shares to issue on the public market and the price it wants to sell them for. When all the shares in the IPO are sold, the company can use the proceeds to invest in the business.