It’s not just the boiling process. The alcohol in beer is anti-bacterial which allows the water to remain safer to drink for longer.
It wasn’t beer in many cases. Grog is basically rum, water, and limes/citrus which help sailors prevent scurvy whilst also protecting them from bacterial infections. I don’t believe they boiled the water at all in that case. I believe it was just mixed all together.