Earth’s magnetic field exerts from earth’s interior to the outer surface.
Inner core is mostly made of iron and temperature is about 5,700°C, at this temperature iron is as hot as surface of sun, but crushing pressure caused by gravity prevents it from becoming liquid and hence cannot produce magnetic field.
But the outer core has a 2,000 km thick layer of iron, nickel, and small quantities of other metals. As the pressure is lower in outer core when compared to the inner core metals exists in fluid state.
Differences in temperature, pressure and composition within the outer core cause convection currents in the molten metal and earths spin result in whirling movements of the liquid metal resulting in generation of electric currents and there by producing magnetic field.