Just like idrios said, the difference is the creation of value. Companies especially public ones, must produce value. If they don't, no one will invest and if they continue not producing value long enough, they go broke and declare bankruptcy.
In a ponzi scheme, money is just moved from new investors to old investors without any value creation. It's impossible to sustain which is why the only interesting question is how long can a ponzi scheme keep going. That's one aspect why Madoff's ponzi scheme was so incredible. It went on for nearly 2 decades (~17 years)!
It's why pyramid schemes are ponzi schemes, they rely on continuously recruiting new people who invest and funnel money up. Once you're in one your goal is to recruit as many people as possible so you're not at the bottom and begin getting money flowing up to you.
Companies (public and private) go bust all the time and do a lot more than move money around. In fact, a company only gets money from its shares through a traditional IPO.
The public markets are second hand markets. If I buy a share of Apple stock, Apple doesn't get another ~$142 (approx its current share price). The person from whom I'm buying the shares gets it.
The only way the company can directly get cash from its stock is by issuing and then selling new stock. Stock fundamentally is a currency the company can control just as the Fed and Treasury control the Dollar. It's an asset which also means companies can take out loans against stock they have as collateral (or they could just sell any extra shares they have to the market).
All of this is also what makes bankruptcy very interesting. A lot of people are owed money, various types of investors, employees etc. Who gets paid, and how much is always the question. Some people by legal right have higher priority than others. Judges play a role in adjudicating this too.
TLDR: Companies are obviously not ponzi schemes. The stock market is just a way for people to benefit from companies' success.