As the ATP is consumed, the muscle cells are unable to keep up with the demand for respiration, O2 becomes limiting, and NADH accumulates. Cells need to get rid of the excess and regenerate NAD+, so pyruvate serves as an electron acceptor, generating lactate and oxidizing NADH to NAD+. Many bacteria use this pathway as a way to complete the NADH/NAD+ cycle.
Slightly unrelated to the topic at hand but did this evolve in species after they started becoming more physically active? I wouldn't think that bacteria would have any use for this pathway within this specific context (generating lactic acid to meet demand for energy due to exercise). Also how important is the production of lactic acid; does the acid itself have benefits or is it simply a byproduct/tradeoff?