Magma forms when the heat content of a particular region exceeds the amount of energy needed to begin the melting process. On earth, the mantle is convecting; that means that the rocks of the mantle are not static but instead these rocks are actually moving. They move up when they are hotter than average and down when colder. This process of heat transfer is called convection and it is the main way the Earth is getting rid of the heat that it started with 4.5 million years ago, as well as the heat that is generated by radioactive decay.
Now when this solid (but plastic) rocks up wells, the pressure decreases and if the pressure decreases enough the rock will begin to melt. The liquid being less dense than the surrounding rock will become buoyant and move upwards eventually erupting on the surface.