Basically, you are right. It is supply and demand. If there are many buyers and few sellers, then the price goes up. And vice versa.
Stocks have value because the corporations are expected to pay dividends from their profits or repurchase the shares.
Also the shareholders elect the management.
The profits are influenced by supply and demand.
If corporations can find or make things to sell and find people to buy them at high enough prices they make profits and their stock goes up.