Bacteria often live within matrix-embedded communities, termed biofilms, which are now understood to be a major mode of microbial life. The study of biofilms has revealed their vast complexity both in terms of resident species composition and phenotypic diversity. Despite this complexity, theoretical and experimental work in the past decade has identified common principles for understanding microbial biofilms. In this Review, we discuss how the spatial arrangement of genotypes within a community influences the cooperative and competitive cell-cell interactions that define biofilm form and function. Furthermore, we argue that a perspective rooted in ecology and evolution is fundamental to progress in microbiology.