That's true, but the shareholders have delegated the decision on how to spend the money to their legal representatives on the board. There's a strong presumption that the shareholders have elected board members whose decisions they support, so if the board makes decisions and the shareholders don't vote them out, those are presumed to express the preferences of the shareholders. Note that those decisions do
not have to maximize shareholder profit. Shareholders
can elect board members who commit to maximizing shareholder value, but they can also elect board members with a variety of other preferences and ideas about how to best run a company, or how to best spend its cash on hand. The board can legally pursue a wide range of strategies, and shareholder lawsuits basically never prevail (in the U.S.) absent some kind of overt wrongdoing, like secret side deals made by board members or something like that, or else shenanigans related to mergers and equity (e.g. some kinds of dilution).
(There's a persistent myth to the contrary, but it's not really rooted in law; see e.g. http://truthonthemarket.com/2010/07/27/the-shareholder-wealt... and http://hbr.org/2010/04/the-myth-of-shareholder-capitalism/ar... ... and even if that weren't true, courts are willing to grant considerable leeway to business strategies such as "building goodwill" and "increasing positive sentiment towards the brand", since courts aren't in a good position to second-guess a duly elected board on such points).
I tend to think of it as the board being in possession of the company that the shareholders have all but signed over to them; with shareholders retaining notional ownership, mainly enforced via the rarely exercised right to revoke the delegation of power if they get sufficiently angry.