And since you own a part of the business, you earn a part of the profit too.
That is exactly how the stock market works.
Companies raise money from the public, and when you buy shares, you own a small piece of that company.
If the company grows, your investment grows.
If the company struggles, your investment can fall.
Simple.