Making the world a better place increases shareholder value and is therefore compatible with their mission ;-)
I think most people dramatically underestimate the amount of resources Google invests in all sorts of programs (including R&D, charitable donations, education, partnerships and so much more) because 1) they don't use it as a negotiating tactic, and 2) the cash cow of the ads business provides an enormously long leash for experimentation and.
Even with Apple, which has more than double the cash (and cash flow), doesn't have this flexibility because of its business model. As a hardware company, volumes, revenue & margin are predictable and anticipated, baked into the stock price with an assumption of -- for the last few years -- dividends. Amazon is at the other extreme. Until very recently, as a result of AWS' runaway success, Amazon had posted negative-to-zero profits for its entire history, reinvesting nearly 100% of free cash flow into business expansion.
No matter what you might think of Google, among the huge tech companies they're unique in their ability to spend enormous sums on things that absolutely do not directly increase shareholder value. The belief within the company, and within the investment community, that some of these moonshots & experiments will eventually stimulate stepwise returns are why they continue to run this way. It's cultural, and ultimately, one of the foundations of "googleyness".
First of all: no jail. This is civil law, not criminal.
Second: it's pretty hard not to have an excuse to do anything you want if you're the board + management. Google, and most other companies, give to charitable organisations, for example. "Now", you'll say, "they're just doing this for PR".
Maybe... But using the PR argument actually works perfectly fine for anything altruistic a company may do.
Unless management explicitly states that they're harming investors for the public good, it's almost impossible to run afoul of the law. Sick pay that exceeds the law? Important for employee retention (and PR). Match your employees' donation to any certified charity? Important to create a community (and PR).
Sick pay is easier to do because it's related to retaining good employees.
But generally, as Milton Friedman famously wrote, "the social responsibility of corporations is to increase their profits".
(just search for [<company> donates], and you'll find examples of charitable donations for every single Fortune 500 company)
I'm not going to argue with the rest of your post because your belief that a company might be doing "anything altruistic" is just sad for me, perhaps laughable for others.