Stock exchanges are secondary markets where existing shareholders can transact with potential buyers. It is important to understand that the corporations listed on stock markets do not buy and sell their own shares on a regular basis. Companies may engage in stock buybacks or issue new shares but these are not day-to-day operations and often occur outside of the framework of an exchange.
So when you buy a share of stock on the stock market, you are not buying it from the company, you are buying it from some other existing shareholder. Likewise, when you sell your shares, you do not sell them back to the company—rather you sell them to some other investor.