there are somany theories and concept behindthis but most accepted one is
Earth's magnetic field is mostly caused by electric currents in the liquid outer core, which is composed of conductive, molten iron. Loops of currents in the constantly moving, liquid iron create magnetic fields. From afar, the Earth looks like a bigmagnet with a north and south pole like any other magnet.