The inner core of the earth is made up of solid iron which is surrounded by hot iron in liquid form which forms the outer core of the earth.
The constant spinning of the earth around its axis creates an electric current in the molten electric core, which in turn produces a magnetic field around the earth.
The swirling motions of molten iron in the earth's outer core are the real reason behind the earth's magnetic field.
Earth's magnetic field also called the “geomagnetic field”, extends to several thousands of kilometres into space.
The earth's magnetic field saves us from the harmful effects of cosmic radiations and the charged particles emitted by the sun.