Bacteria forms spores (technically "Endospores") when the environment they are in is slowing becoming inhospitable. A live rock for example, will cause the bacteria to form endospores as the rock dries and the salinity of the water in the rock skyrockets.
These endospores are very hardy and...