When CdTe solar cells are doped with Cl, the grain boundaries no longer act as recombination centers but actively contribute to carrier collection efficiency. The physical origin of this remarkable effect has been determined through a combination of aberration-corrected scanning transmission electron microscopy, electron energy loss spectroscopy, and first-principles theory. Cl substitutes for a large proportion of the Te atoms within a few unit cells of the grain boundaries. Density functional calculations reveal the mechanism, and further indicate the grain boundaries are inverted to n type, establishing local p-n junctions which assist electron-hole pair separation. The mechanism is electrostatic, and hence independent of the geometry of the boundary, thereby explaining the universally high collection efficiency of Cl-doped CdTe solar cells.