The Earth behaves as if there is a giant bar magnet running through its middle from pole to pole. This
affects every magnetic material that comes within its reach. If you hold
a magnet so that it can rotate freely, it always ends up with one end
pointing to the Earth's North Pole and the other to the South Pole. This
is how a compass works- the needle automatically swings to the North.
The Earth's magnetism comes from its inner core of iron and nickel. You
can use the compass you make here to plot a magnetic field like the Earth's.
The Earth's magnetic field is slightly tilted, so compasses do not swing
exactly toward the North Pole, but to a point a little little way off
from northern canada. This direction is known as magnetic north.

Magnetic Protection

The effects of Earth's magnetism extend 37,000 miles out into space. In fact,
there is a vast magnetic force field around the Earth called the magnetosphere.
This traps electrically charged charged particles and so protects the
Earth from the solar wind - the deadly stream of charged particles hurtling
from the Sun.