When magma rises from deep inside the earth and explodes out of a volcano, it is called lava. Lava cools quickly on the surface and forms rocks. Rocks formed in this way are called extrusive igneous rocks.
When these rocks are formed, their minerals possessing magnetic properties align with the magnetic field of the Earth. When the rock becomes solid, the crystals remain aligned to the magnetic field of Earth.