
Jack X. answered 08/05/20
20+ year veteran of financial industry
The basic question is always the best and the hardest question
The stock market index you see are backed by the market value of all the companies. The market value of a company roughly speaking is how much it will cost to buy all the ownership of this company. For example, microsoft market value is $200 billion. Underneath the market value is the book value. The book value (also called shareholder equity, net asset..) roughly speaking is the current worth of the company, meaning if hypothetically all the owners decide to resolve the company, just sell everything (assuming no fire sale) to cash, how much roughly in total the owners will get back after paying back all the creditors. For example, the microsoft book value is about $117 billion
Market value is normally higher than book value, and the ratio is the PB ratio
Market value = PB * book value.
Why market value is normally higher than book value? Because the book value of a profitable company always grow over time. Why? Because the current net worth of a company increases by the amount of net income. (assuming the company is not raising new equity, buying back shares, or paying dividends)
Your question is why market value grows over time. Mainly, this is because Book value is increasing over time from accumulating all the positive earnings. Microsoft's book value has increased greatly because of the earning accumulated over time. So is Apple, so is Exxon, so is AT&T etc.... Of course, increase in PB can also cause market value to go up, but this part does not always increase as the book value. When more people crazy about getting rich in the stock market, PB becomes higher. It swings widely during market hype and crash.
Lastly a comment on your thought. Money stays with the commercial banks (cash bills people carry is a tiny amount of the money in our country). It doesn't go outside of the banks before or after someone buys a stock. Money changes from one bank account to another at the bank. It does not go from a "real economy" to "the stock market". There is no such distinction for money. What is true is that if more people are buying stocks, then PB can be higher, or else lower. But still, even no one buys stocks, the book values of the companies will still grow as long as they are all making positive net income.