Cosmic rays are charged particles moving at high speeds. As such, they interact strongly with the magnetosphere of a planet, as per the Lorentz force law in the absence of electric fields:
As you can see, at such high speeds, even a weak magnetic field has a strong influence on a particle's trajectory. Due to the way the cross product in the above expression works, the Lorentz force tends to cause particles to tightly orbit magnetic field lines. From the picture included in the question, you can see that if we "pin" particles to magnetic field lines, they only hit the Earth at two places: the north and south magnetic poles. Thus, the rest of the Earth is protected from cosmic rays.
Side note: When the cosmic rays, following the magnetic field lines, hit the atmosphere above the poles, the energy of these interactions is converted to the shimmering light of the aurora borealis (in the north) and aurora australis (in the south).