- new companies go IPO, they do it because there is liquidity in the market, so they can count on someone buying their shares and cashing in.
- some blue-chip companies pay out dividends
- as much as many people hate on that, profitable companies engage in shares buy backs which is a way to return money earned by the company to its shareholders. If the company makes no profit, it has less to do buy-backs with.
- in the end a share is a piece of a company, big part of their long term valuation will always be driven by their fundamentals like a ratio between profit and revenue or how likely they are going to default on their debts, no one wants to own a piece of company that is going to disappear
in summary, there is quite some connection to the real economy, but I agree that part of the valuation is just because other people pour money in it is just not that bad.