do public companies always have to do what makes the shareholders the most money or risk being sued?
I know the board has some fiduciary duty, but can a company put some guardrails on it when they go public, like saying the environment will always come first, or employees or customers or something?
Company law varies depending on local law. There is no general answer to this that is broadly or even approximately applicable on an international level.
In the United States, however, some states allow a company to be registered as a benefit corporation which allows for "the good of society" to be regarded as part of the company's best interest, in addition to profit. Among these states is Delaware, which is where the vast majority of large American companies are registered.
Edit: To add, American company law generally abides by the principle of "no harm, no foul". A company's directors can make decisions that aren't necessarily the most profitable if its shareholders are okay with those decisions. As an example, the hamburger chain In-N-Out Burger pays its employees the highest wages and gives them among the best benefits in the fast-food industry, and they refuse to do franchising. This probably isn't the most profitable way to run their business, but since the shareholders, the one family that owns the entire business, are okay with it, that's how the company is run.
What this means is that you can simply give shares to the people you want to benefit, and in doing so, you could argue that you are working in the best interests of those shareholders. For example, credit unions in the USA, which are essentially not-for-profit banks, give shares to every one of their customers, who are required to purchase one and only one nominal share (usually valued at $5) to open an account. This share is non-transferrable and is redeemed and their "investment" is refunded when they close their last account. The entirety of a credit union's shares are issued this way, so each customer has one and only one share. In that way, when the directors keep fees low and interest paid high, they can't be argued to be breaching their fiduciary duty, because their customers are all also sharehholders.
This probably isn't the most profitable way to run their business, but since the shareholders, the one family that owns the entire business, are okay with it, that's how the company is run.
This is why we often see privately owned companies making better long term decisions compared to publicly traded companies.
Credit unions are the American equivalent of building societies. Building societies have a historic emphasis on mortgages (hence their name) but over time have morphed into de facto banks after legislation passed in the 1980s. Credit unions in America have always started out as thrift and lending institutions and so their evolution into what they are today was more natural than how building societies came to be in the UK. However, credit union membership was initially much more restricted.
There is, however, one interesting difference. In the UK, building societies are permitted to "demutualise" and convert themselves into regular limited companies. When this happens, the members receive shares in the new company and it is then operated like any other company. In the United States, this is not allowed. There is no process for a credit union to convert into a for-profit entity.
Credit unions in the US grew a lot once the US federal government started issuing federal credit union charters that had lax membership requirements. Originally, credit union members had to share some common bond. Thus, it was common to find credit unions attached to trade unions or employers or heavily localised (such as to a single farming community). For example, the largest credit union in America, Navy Federal Credit Union, had a strict membership requirement wherein only servicemembers, retired servicemembers, and their family were allowed to be members. Many credit unions today have only nominal membership requirements (e.g. live within 100 miles of a branch or something else to that effect) and thus almost anyone can join.
Edit: I would like to add that credit unions are not a uniquely American phenomenon, nor did they originate from the US, but credit unions are quite popular in the US. Similarly, neither are building societies restricted to the UK.
Legally speaking, shares determine ownership of a business entity. An entity must have one or more owners. If nobody owns something, then the State owns it. This is called "escheat". So the shares prevent that.
A law could be enacted that would eliminate the need for shares in credit unions, but so far, that hasn't been done, at least not in my home state of Oregon. Again, laws vary.