Earth's magnetic field is generated by the convective movement of its ~2,250 km thick outer core, composed of molten iron and nickel.
Heat is generated by potential energy released by heavier materials sinking toward the core and by decay of radioactive elements.
The heat creates convection currents in the molten metal, which are organized into rolls by earth's rotation and the Coriolis force. This creates electric currents which generate the magnetic field.