The function of bacterial endospores, according to Cornell University, is to allow the survival of bacteria's cell lines through harsh conditions that would kill a normal member of the species. These conditions include starvation, ultraviolet light, dessication and chemical damage. The endospore is an unusually small and largely dehydrated bacterium with a germ cell wall, surrounded by further walls of protein and peptidoglycan that protect it.
The process of creating an endospore involves a unique kind of cell division. The mother cell replicates its DNA and undergoes cytokinesis, but one of the daughter cells is much smaller than the other. The larger daughter cell then envelops the smaller daughter cell, which becomes a forespore within the larger cell. The larger cell forms the walls of peptidoglycan and protein around the smaller cell, then lyses, leaving only the endospore behind to weather whatever hostile conditions prompted its creation.
Once more favorable conditions begin, the endospore transforms its peptidoglycan shell into a regular cell wall and takes in water to become a normal member of its species. While most species of bacteria that form endospores do so in response to unusual environmental conditions, others form endospores regularly, even on a daily basis, due to day and night conditions or other regular occurrences in their environments.