People buy partial ownership of a company in form of share so as to receive dividend from the company along with the capital gains which they will get when they will sell their share at higher price.
Along with accumulated profits it’s the capacity of the company to grow and generate profits in future which increases its valuation. As valuation increases, demand of the share also increases.
As more people seek ownership of share, the demand increases as compared to the supply, hence the share price increases.
People then sell their shares at higher price thereby benefitting from capital gain thru price appreciation.
Similarly, if performance degrades, value decreases leading to more selling. As more people want to sell, the supply increases thereby price reduces.