I'll take a shot from the hip at this question, but note that I won't add my customary citations or links.
The stock market is the paragon of property and trusts, contracts, corporations and law, and the capitalist socio-economic system. The very existence of the stock market implies a society that has some or most of these concepts.
For example, for shares to be traded, there generally must exist ownership rights upon the shares, distinct from the ownership rights that the company has of its own property. Or if not outright ownership of a share, then the benefit that a share provides (eg dividends). It also implies a legal system that will enforce these rights and the obligations of the company to its shareholders.
For a tradable company to exist, it must be organized/chartered as an entity distinct from any single person. This is different than the feudal days, when ventures would be undertaken "in right of the King" or some member of the nobility. The feudal method wouldn't work for modern companies, or else the King/Duke/Count/whatever could stiff the shareholders by just taking all the earnings. The company still needs to be created by legal means, either an Act of Parliament/Congress, by letter patent from the Monarch, or the modern administrative method of applying to the state Secretary of State (USA) or Companies House (UK) as examples.
Even the structure of a for-profit tradeable company -- when compared to a state-owned enterprise, a non-profit, a co-op, or an NGO or QUANGO -- is a representation of the values inherent to capitalism. A company is obliged to use the shareholders' funds -- which is held by the company but is owed to the shareholders -- to extract the greatest return. But this can come in many forms.
Short-term value from buying investments and quickly flipping them (eg corporate home buyers) is different than rent-seeking (eg corporate landlords) and is still different than long-term investments that actively work to build up the value (eg startup incubators, private wealth funds, Islamic banking, transit-owned adjacent property). If a for-profit company doesn't have a plan to extract a return... they're in hot water with the shareholders, with penalties like personal liability for malfeasance.
Another way of looking at the stock market is that if you have all the underlying components but don't yet have a stock market, it would soon appear naturally. That is to say, if the public stock markets were banned overnight, shares would still trade but just under the table and without regulation. But if any critical part underpinning the markets stopped existing, then the market itself would collapse.
History shows numerous examples where breakdowns of the legal system resulted in market mayhem, or when corporate property is expropriated for the Monarch's wars or personal use, or when funds invested into or paid out of companies is hampered by terrible monetary inflation.
As for what the stock market does, its greatest purpose is to organize investments into ventures. Historically, ventures were things like building a ship to sail to the New World and steal obtain goods to sell at home. Merchant ships were and are still very expensive, so few singular persons could afford it. And even if the could, the failure of the venture could be catastrophic for that person's finances. Better to spread the risk and the reward amongst lots of people.
What was once the sole domain of the landed gentry and nobility, slowly opened to the nouveau riche during the Industrial Revolution(s), then in turn to everyday people... for better or worse. It's now almost trivial to buy a share in any particular listed company, but just opening the stock market to everyone would have been chaotic at best. I think it's NYSE that still has on-floor traders/brokers, but imagine if all shares in that market had to be traded in a single room, with no digital trading. It's already quite lively on the trading floor today, now add all the trades from middle class Americans on payday. It would become physically impossible.
Likewise, a pure capitalist stock market would permit awful things like bribing journalists to write fake stories to crash a stock, then buy it for cheap. Or pump and dump scams. And would have no "circuit breakers" that halt a share during so-called flash crashes.
I'm reminded of a scene from the ITV show Agatha Christie's Poirot in the episode "Appointment With Death", where a wealthy woman is not only murdered but her business empire collapses because the murderer also spooks the markets as a double whammy, causing investors to panic and sell up. The relevant implications here is that despite her company not having changed its financial picture, it got cut up for scrap and thus lost most of its value, rendering the business worthless in the end. Companies are usually valued more as a going-concern, above what all its property put together would amount to. Where does that additional value come from? It's the prospect of a return from this particular assemblage of resources.
Suffice it to say, the stock market is a lot of things. But I view it as a natural result of certain other prerequisites, meaning we can't really get rid of it, so instead it should be appropriately regulated.