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.