First, there is a definition we need to make. Just to keep things straight, geologists use the word "magma" for molten rock that is still underground, and the word "lava" once it has erupted onto the surface.

Rocks in the mantle and the core are still hot from the formation of the Earth about 4.6 billion years ago. When the Earth formed, material collided at high speeds. These collisions generated heat (try clapping your hands together - they get hot) that heat became trapped in the Earth. There is also heat within the earth produced by radioactive decay of naturally-occurring radioactive elements. It is the same process that allows a nuclear reactor to generate heat, but in the earth, the radioactive material is much less concentrated. However, because the earth is so much bigger than a nuclear power plant it can produce a lot of heat. Rocks are good insulators so the heat has been slow to dissipate.

This heat is enough to partially melt some rocks in the upper mantle, about 50-100 km below the surface. We say partially melt because the rocks don't completely melt. Most rocks are made up of more than one mineral, and these different minerals have different melting temperatures. This means that when the rock starts to melt, some of the minerals get melted to a much greater degree than others. The main reason this is important is that the liquid (magma) that is generated is not just the molten equivalent of the starting rock, but something different.

You could think of making a "rock" out of sugar, butter, and shaved ice. Pretend that they are mixed equally so that your rock is 1/3 sugar, 1/3 butter, and 1/3 shave ice. If you start melting this "rock", however, the "magma" that is generated will be highly concentrated in the things that melt more easily, namely the ice (now water) and butter. There will be a little bit of molten sugar in your magma, but not much, most of it will still be crystalline.

The most common type of magma produced is basalt (the stuff that is erupted at mid-ocean ridges to make up the ocean floors, as well as the stuff that is erupted in Hawai'i). Soon after they're formed, little drops of basaltic magma start to work their way upward (their density is slightly less than that of the solid rock), and pretty soon they join with other drops and eventually there is a good flow of basaltic magma towards the surface. If it makes it to the surface it will erupt as basaltic lava.