Money flows into the market when people earn it and use it to buy stocks. Every stock purchase is matched by a stock sale, but most stock sales turn around and become new stock purchases. That's a net long term inflow of money into the stock market, from people purchasing stocks with their earnings (often, via retirement funds).
Most of the actual trading is traders trading to each other, and that shouldn't raise the market cap long term (though it does create volatility). But there is also real inflow of money into the market.
As industrialized countries age, there will be relative more people in retirement compared to people in their working years. As a result, the money being pulled out of the stock market will grow relative to the money being put into the market. That is one of the reasons that we shouldn't expect to see the same stock market returns as in the past.
Most of the actual trading is traders trading to each other, and that shouldn't raise the market cap long term (though it does create volatility). But there is also real inflow of money into the market.