It has been thought that the capture of irregular moons-with non-circular orbits-by giant planets occurs by a process in which they are first temporarily trapped by gravity inside the planet's Hill sphere (the region where planetary gravity dominates over solar tides). The capture of the moons is then made permanent by dissipative energy loss (for example, gas drag) or planetary growth. But the observed distributions of orbital inclinations, which now include numerous newly discovered moons, cannot be explained using current models. Here we show that irregular satellites are captured in a thin spatial region where orbits are chaotic, and that the resulting orbit is either prograde or retrograde depending on the initial energy. Dissipation then switches these long-lived chaotic orbits into nearby regular (non-chaotic) zones from which escape is impossible. The chaotic layer therefore dictates the final inclinations of the captured moons. We confirm this with three-dimensional Monte Carlo simulations that include nebular drag, and find good agreement with the observed inclination distributions of irregular moons at Jupiter and Saturn. In particular, Saturn has more prograde irregular moons than Jupiter, which we can explain as a result of the chaotic prograde progenitors being more efficiently swept away from Jupiter by its galilean moons.