To answer this question, you must look very carefully inside the caterpillar. Inside its body are some very special cells called imaginal discs. These discs will become the structures of the adult butterfly. The discs are very small in the caterpillar.
When the caterpillar becomes a pupa (or chrysalis), which is the stage right before it becomes an adult butterfly, it dissolves most of its own body in order to feed these imaginal cells. If you opened up the pupa at this point in time, it appears as if it was melted on the inside. However, despite the appearance of tomato soup, the imaginal cells are still there, ready to spring into action. The cells undergo a tremendous amount of growth inside the pupa. Some discs will become the wings (there are four discs for each wing of the butterfly), and others will become the main body parts, including the head, antennae, legs, thorax (middle body segment) and abdomen.