In case of Earth, Wikipedia describes [1] it as being "[..] generated by electric currents due to the motion of convection currents of a mixture of molten iron and nickel in Earth's outer core". This makes Earth a geodynamo [2]. (The aforementioned Wikipedia page is actually really long and detailed, a lot more than I would have thought)
[1] https://en.wikipedia.org/wiki/Earth%27s_magnetic_field [2] https://en.wikipedia.org/wiki/Dynamo_theory