Ships are registered under foreign flags for a variety of reasons.
labour laws. Many countries where shipping firms are at home have laws that make hiring staff much more expensive than would be the case in say Liberia. Work conditions, salaries, etc. would make them uncompetitive.
appearances. A Taiwanese firm wishing to do business with mainland China would want to do so through a front company in a third country. In fact for a long time (maybe still) it was illegal for companies doing business in Taiwan to do business in mainland China by PRC law. This caused many airlines to create subsidiaries to handle traffic to one or the other to circumvent this.
as you fear, an easy way to get out from under safety and environmental regulations. But that’s probably a minor reason as many nations enforce those on any vessel docking in their ports (or even transiting their waters), not just on ships registered there.