Metapopulation models have been instrumental in quantifying the ecological impact of landscape structure on the survival of a focal species. However, extensions to multiple species with arbitrary dispersal networks often rely on phenomenological assumptions that inevitably limit their scope. Here, we propose a multilayer network model of competitive dispersing metacommunities to investigate how spatially structured environments impact species coexistence and ecosystem stability. We introduce the concept of landscape-mediated fitness, quantifying how fit a species is in a given environment in terms of colonization and extinction. We show that, when all environments are equivalent, one species excludes all the others-except the marginal case where species fitnesses are in exact trade-off. However, we prove that stable coexistence becomes possible in sufficiently heterogeneous environments by introducing spatial disorder in the model and solving it exactly in the mean-field limit. Crucially, coexistence is supported by the spontaneous localization of species through the emergence of ecological niches. We show that our results remain qualitatively valid in arbitrary dispersal networks, where topological features can improve species coexistence by buffering competition. Finally, we employ our model to study how correlated heterogeneity promotes spatial ecological patterns in realistic terrestrial and riverine landscapes. Our work provides a framework to understand how landscape structure enables coexistence in metacommunities by acting as the substrate for ecological interactions.