I wrote a post on this a few years ago. There's a few different roles that capital markets play, but I think the big one in terms of real economic value is probably warehousing. The financial markets - stock market, bond markets, etc - provide value mainly by warehousing credit. (Here I mean credit in a fairly general sense, including any sort of expected value at a later time in exchange for funds now - e.g. stocks are included, bonds are included, futures are included, etc.)
This provides value in much the same way as warehousing grain: when there's a shortage of grain, the grain warehouses can can provide grain for a little while (albeit at a higher price) to avoid starvation. When there's a grain surplus, the warehouses can buy up excess (albeit at a lower price) to avoid spoilage/waste. They smooth out the grain supply in time, and that's how they make money. Same with credit: when there's a shortage of credit, markets crash, and people/companies are desperate for cash. Those investors who were warehousing cash sell it, buying low-priced stocks/bonds/etc in exchange. When there's a surplus of credit, asset prices go back up, and the investors sell their assets off. They smooth out the credit supply in time, and that's how they make money.
Again, this isn't the only way that financial markets provide value; see the linked post for more. But I do think it's the main way.
That isn't always true. Sometimes, when a company wants to raise capitol, they sell their own stock, and you, in buying the stock, are directly giving the company capitol. In that case, the market price, which has been determined by all of this buying and selling, determines the allocation of voting power (and dividends if there are any) between the owners of the older shares of stock and the buyers of the new shares of stock.
Jack Bogle, the creator of the first index fund, says
My response to this has always been... if that's true, what is the point in all of this? It's a mechanism that predicts the success of companies, but plays only a very small role in investment? Could we get that money to do something better, then?