There are 2 different issues here. The stock market was originally devised as a perfectly legitimate method of raising money for companies and providing value measurements and liquidity to the investors. But there has always been a tendency to turn it into raw speculation. For example, with things like day trading.
If you are Warren Buffet who only buys stocks in companies that he believes will grow quickly and produce increasing streams of profits, and you buy significant shares of these companies as a percentage of your own portfolio and as a percentage of the company's market cap, then you are not speculating, you are investing. But if you are watching the minute-by-minute, day-by-day ups and downs of stocks for which you only know the ticker symbol and aren't even sure what business the company is in, then you are gambling, just like being in Las Vegas.
It's also worthwhile to remember that Wall Street takes a "house cut" just as substantial as Las Vegas. The "overhead" costs will kill you if you aren't lucky when you're gambling on Wall Street, as opposed to investing. That's why in the classic book on Wall St. investing (whose title I momentarily forget), on the first page he takes you to the harbor in Manhattan and shows you all the yachts belonging to the brokers and reminds you that there are precious few that belong to the "investors."