Stromatolites are formed by the trapping, binding, and cementation of sediment grains by microbial mats, primarily cyanobacteria. These mats grow in shallow, warm, saline waters and produce a sticky layer of mucus that traps sediment. Over time, these layers build up, forming the distinctive layered structures characteristic of stromatolites.