When a company makes an Initial Public Offer (IPO), the proceeds of such IPO goes to the company issuing the IPO. This is the time the shares are said to be part of the ‘Primary market’.
Once a share gets listed on the Stock Exchange, its said to be part of the ‘Secondary market’ the stock-money exchange happens between public investors.
To cite an example, assume company ‘X’ came up with an IPO and if you subscribe to this IPO and get allotment, the money that you pay would go to company ‘X’ [primary market].
Once the shares are allotted to you and the shares are listed on the Stock Exchange, you are free to sell it on the Stock Exchange [secondary market] where the stock-money exchange happens between you and the buyer of the share.
The money goes to the seller, who have sold you the shares. It will not go to the company.
The company gets the initial fund required thro IPO.
But company keeps certain % of share quantity with the promoters.
When the shares are getting traded openly in the stock exchange, say the share price increases. This increase in share price is settled between buyers and sellers. The company does not gain from this. The only gain what company attains is its brand value (market cap increase) and share price/value appreciation with the promoters shareholding.