You mean like I already did?
You’re getting confused or you might not actually understand how companies work, so I’ll break it down.
There is no law forcing a company to profit. (Though Companies are generally formed for that purpose.) A private organization could do whatever it wants within legal bounds. (This is how non profits, charitable foundations etc exist.)
But, what happens next is many companies go “public” by selling shares. In essence, they put a percentage of themselves on the market and people by shares in that company, such that they, legally speaking, own a tiny percentage of that company. Part of that purchase is that the company now has a fiduciary duty to the shareholders. As noted before, a duty is a legal concept like assault, negligence etc. And I explained fiduciary duty earlier, you can look through.
Here is kind of a classic example of a company losing a case because its directors breached their fiduciary duty to minority shareholders:
Why do you think people buy shares? Just fans of three digits and numbers that change?
Edit: Italicized the relevant section to make things easier for you.