That, however, doesn't last forever. In the conditions of the 18th century or whatever, microorganisms will get into the beer after mashing/boiling, so the heat treatment only helps for a while. The fermentation really does protect the beer afterwards, but it's a combination of low pH, alcohol, low oxygen, little nutrients, CO2, etc. Hops also help against gram-positive bacteria.