The shape of bacteria is influenced by their genetic makeup, the structure of their cell wall, and their environmental adaptations. Rod-shaped bacteria (bacilli) often have an advantage in motility and surface attachment, while spherical bacteria (cocci) are more resistant to mechanical stress.