Inner core heats the outer core within the Earth producing convection currents
These currents are influenced by the coriolis effect which therefore become aligned close to the Earth's geographic poles.
Liquid iron alloy within the outer core moving within these circulations act as a current- producing an electromagentic field.
Similar to a magetic bar- dipole pointing from the North to the South.
Dipoles can flip from Normal (pointing North to South) to reversed (South-North).
When rocks are formed during cooling from magma, magnetic elements e.g magnetite aligned with the current dipoles where they are then preserved in the rock.