The response of an ecosystem to perturbations is mediated by both antagonistic and facilitative interactions between species. It is thought that a community's resilience depends crucially on the food web---the network of trophic interactions---and on the food web's degree of compartmentalization. Despite its ecological importance, compartmentalization and the mechanisms that give rise to it remain poorly understood. Here we investigate several definitions of compartments, propose ways to understand the ecological meaning of these definitions, and quantify the degree of compartmentalization of empirical food webs. We find that the compartmentalization observed in empirical food webs can be accounted for solely by the niche organization of species and their diets. By uncovering connections between compartmentalization and species' diet contiguity, our findings help us understand which perturbations can result in fragmentation of the food web and which can lead to catastrophic effects. Additionally, we show that the composition of compartments can be used to address the long-standing question of what determines the ecological niche of a species.