There's something you've overlooked: companies are ultimately run by humans. Humans who make decisions at all levels of the hierarchy, in order to further their own personal priorities, even if it dramatically conflicts with the shareholders' goals of maximizing profits.
In some cases, the personal priorities can be personal advancement. We see this all the time when managers hire/promote their friends, sexually harass their subordinates, and make decisions on the basis of politics as opposed to technical/business merit.
And in other cases, the personal priorities can be moral values. Values such as promoting free speech, fighting censorship, protecting consumer rights, and avoiding layoffs.
The idea that every single decision taken by a company is perfectly optimized to maximize long term profits, is baseless. There simply does not exist any mechanism to monitor and enforce such a requirement. The shareholders have only one lever to pull: accept the current leadership team, imperfections and all. Or fire them and risk destroying the company in the resulting churn. This gives both the executives and employees tremendous leeway to prioritize values over profits, as long as they are good enough to not get fired.