Lactic acid. When muscled burn glucose without oxygen (unaerobically) to produce energy needed for exercise, a bi product called Lactic Acid is produced. When this substance accumulates, it causes pain and cramping. As you become more fit, your body uses the aerobic (oxygen) pathway more to produce the energy it needs, so less need to use of the unaerobic path--less Lactic Acid is produced--less pain/crams.
Microtears. There are two theories on muscle soreness after exercise. One theory is that Lactic Acid buildup from anaerobic metabolism causes early pain and cramping. Another theory is that delayed muscle soreness occurs because of microtears to the muscle. These tears heal with rest and the better conditioned you are, the less sore you will be after exercise.