The ability to resolve spatially and identify chemically atoms in defects would greatly advance our understanding of the correlation between structure and property in materials1. This is particularly important in polycrystalline materials, in which the grain boundaries have profound implications for the properties and applications of the final material2. However, such atomic resolution is still extremely difficult to achieve, partly because grain boundaries are effective sinks for atomic defects and impurities3, 4, 5, which may drive structural transformation of grain boundaries and consequently modify material properties6, 7. Regardless of the origin of these sinks, the interplay between defects and grain boundaries complicates our efforts to pinpoint the exact sites and chemistries of the entities present in the defective regions, thereby limiting our understanding of how specific defects mediate property changes. Here we show that the combination of advanced electron microscopy, spectroscopy and first-principles calculations can provide three-dimensional images of complex, multicomponent grain boundaries with both atomic resolution and chemical sensitivity. The high resolution of these techniques allows us to demonstrate that even for magnesium oxide, which has a simple rock-salt structure, grain boundaries can accommodate complex ordered defect superstructures that induce significant electron trapping in the bandgap of the oxide. These results offer insights into interactions between defects and grain boundaries in ceramics and demonstrate that atomic-scale analysis of complex multicomponent structures in materials is now becoming possible.