Igneous rocks are formed through the cooling and solidification of magma, either on the earth's surface or beneath it. When magma cools on the surface, it becomes extrusive, often resulting in rocks like basalt, which may contain vesicles formed by trapped gas bubbles.