Lemmy uses a open source license called the AGPL, which is a type of reciprocal license (called copyleft), which basically means you can't close the source without everyone who contribute agreeing to do it or rewriting their contribution (and i don't think it ever happened, i think it can be extremely difficult ).
setting up a non profit that that has a decentralized power structure and is legally obligated to do public good might also provide another protection (but a copyleft license is probably enough).