Magma rises to the Earth's surface due to a combination of differences in density with other rocks in the crust and pressure. The differences in density cause it to move upward until its density is the same as the other rocks in the crust. It then builds pressure, causing it to rise to the surface.
Magma is made of molten rocks and minerals. It forms in the lower crust and upper mantle layers of the Earth due to movements in the mantle, changes in temperature or contact with water or carbon dioxide under the surface. These changes cause rocks in the crust to melt, forming magma.
Sometimes, magma collects in magma chambers, and sometimes, it simply rises until its density is the same as the other rocks around it. However, once it has risen, it comes into contact with gases, forming bubbles. In some cases, the magma decompresses too quickly and the bubbles build up and create pressure. This pressure causes the surrounding rock to fracture, letting the magma rise to the surface.
Magma that is created through exposure to water can rise under different conditions. Water causes rock to melt at lower temperatures, so less pressure is involved. However, water usually enters the lower crust through subduction zones in the ocean floor, which also allow the magma to escape.