Where does magma come from? Melted rock.
Why does rock melt? Several factors can come into play:
As you know, the core of the Earth is super-hot, and that heat is transferred up through the mantle to the relatively cooler outer part of the Earth. This transfer varies in intensity from place to place. Generally, it is called the geothermal gradient, and is in the range of 20 to 30 degrees C per kilometer of depth (it gets hotter as you go down, of course).
At a depth of about 100 kilometers, the rocks are hot, in the range of 1200 to 1400 degrees C. If you take a look back at those diagrams in the last section, you'll see labels indicating melting of silicate rocks would occur in the range of 200 to 1200 degrees C. So, you might expect that, at 100 kilometers, the upper mantle would be all melted, as with the core. But, of course, it isn't totally melted way up there; it is solid rock. The reason why it stays solid, even though its hot enough to melt, is that the weight of the overlying material above 100 kilometers makes the pressure so high that melting is not allowed. That should make sense: if something is under more pressure, it is harder for it to melt. So, if in places pressure is somehow reduced, but with the temperature still warm enough, rocks will melt. Along spreading ridges on the ocean floor, underneath them, mantle rocks are being upwelled; mantle rock is being brought upward, toward the surface, and it is quite hot. The upwelling reduces the pressure on the rocks, which are peridotite, mainly made of olivine, and melting occurs. The mafic magma that quietly comes up and out along the spreading ridge's central rift valley is generated in this fashion, and solidifies to form gabbro and basalt, making up new oceanic lithosphere along these divergent plate boundaries.
Have you ever made homemade ice cream with an old-fashioned ice cream maker? Some you have to crank by hand, others have an electric motor. You mix milk and sugar and other stuff to make the ice cream, but you need to get it cold enough to freeze, so you add ice around the mixture to cool it down. But it doesn't get cold enough fast enough, so you ap salt to the ice to make it melt faster, and to bring the temperature down faster. Salt acts like a volatile in this instance; it helps the ice to melt. Water will do the same sort of thing to ice. The presence of water and dissolved chemicals in rock will help it melt. Dry rock will not melt as easily. An important process of magma creation happens along subduction zones, where water gets in the nooks and crannies and fractures of oceanic lithosphere along the sea bottom, and then goes along for the ride where oceanic lithosphere is subducted. As the plate is subducted, the temperature goes up, as usual. The presence of water, and other volatiles, helps the descending slab of lithosphere to melt, not totally, but enough to generate magma. This is called partial melting. The rock in oceanic lithosphere has an overall mafic composition, because it was formed along a spreading ridge (see previous paragraph). If the oceanic lithosphere were totally melted, the resulting magma would, in turn, be mafic. But with partial melting, the more siliceous end of the mineral spectrum is melted off, helped by water and volatiles, and the resulting magma along subduction zones is intermediate in composition (more silica, by proportion, than mafic).
So, there are two linear boundaries where magma is generated on a wide scale:
We also have more localized places where magma is generated. One important place is above a deep-seated mantle hot spot. A hot spot is a location on the sea floor or land surface that gets heated up by concentrated heat flow from deep down. There are hot spots around the globe, here and there, some under oceanic lithosphere, some under continental lithosphere. There is a hot spot under the Hawaiian volcanoes. There is a hot spot under Yellowstone. The melting that occurs is primarily the result of intense heat flow in these locations.