No, it's not gambling. It's people coming together in order to build something. And a place where people can sell their stocks is a stock market.
There are many problems in this world. But there existence of stocks and the fact that they can be traded is not one of them.
Early investors geht stocks. And they can even sell them before the company goes public (if it ever does). The IPO or direct listing or whatever process is chosen, brings the stocks to an open exchange. And a company usually issues new stocks during an IPO and can issue new shares at any time.
The fact that IPOs are overpriced and kinda shady is true, but that's one of those problems I meant, which are not explicitly due to the existence of stocks.