Earth acts like a giant magnet due to its molten iron core creating electrical currents. The magnetic North Pole is near the geographic North Pole (though they drift and aren't identical). This magnetic field is why compasses work and creates the magnetosphere.