Our magnetic field protects us from blasts of solar radiation & likely existed soon after the planet formed. This may explain why Earth is habitable & Mars is not. Without our magnetic field, solar winds would strip away Earth's atmosphere & oceans.
Back-to-Back Sun Storms May Supercharge Earth's Northern Lights
An artist's impression of the magnetic fields of Venus, Earth and Mars interacting with charged particles streaming from the sun. Earth's magnetic field protects it from these atmosphere-stripping particles.
A Timelapse Of Our Planet's Surprisingly Turbulent Magnetic Field