Cooperative organisms are ubiquitous in nature, despite their vulnerability to exploitation by cheaters. Although numerous theoretical studies suggest that spatial structure is critical for cooperation to persist, the spatial ecology of microbial cooperation remains largely unexplored experimentally. By tracking the community dynamics of cooperating (rpoS wild-type) and cheating (rpoS mutant) Escherichia coli in well-mixed flasks and microfabricated habitats, we demonstrate that spatial structure stabilizes coexistence between wild-type and mutant and thus facilitates cooperator maintenance. We develop a method to interpret our experimental results in the context of game theory, and show that the game wild-type and mutant bacteria play in an unstructured environment changes markedly over time, and eventually obeys a prisoner's dilemma leading to cheater dominance. In contrast, when wild-type and mutant E. coli co-inhabit a spatially-structured habitat, cooperators and cheaters coexist at intermediate frequencies. Our findings show that even in microhabitats lacking patchiness or spatial heterogeneities in resource availability, surface growth allows cells to form multi-cellular aggregates, yielding a self-structured community in which cooperators persist.