How does the allegory of the cave relate to the theory of form?
The allegory of the cave is probably the most famous of all the passages in the Republic. It is used to show the difference between the world of senses and the world of forms. Forms are immutable and timeless, unlike what we see/feel/sense in the actual world, which are shadowy, unreliable reflections of their Forms. Plato believes that humans can never really see the Forms, but there are some that are able to apprehend them more clearly than others.
The allegory he uses in the cave is that the prisoners who are chained to the wall and forced to watch the shadowy figures and scenes that can be seen through a curtain—the shadowy figures (which we know are not real) become their view of reality.
If a prisoner was released and forced to see the "real" world as we know it, the brilliance of it would be painful and difficult for the prisoner to understand. At first, the man would want to return to the cave and the comfort of his own reality, but once he became used to it, he would eventually realize that he had been living a life of illusion in a world in which he didn't even know that the sun existed. He may go back and try to teach his fellow prisoners about his new reality, but if he attempted to release them to experience what they would see as madness, they would try to kill him.
Plato uses this allegory to explain why philosophers are so often mocked: they have had a glimpse of the Forms, and the people to whom they are trying to explain those ideas find them incomprehensible. Most people are stuck in the shadowy world of the senses, like the prisoners in the cave.
The Theory of the Forms is a key part of Platonic idealism. For Plato, reality is grounded in a still higher realm of universal truths, which we, trapped in our material existence, can only access indirectly. While we might have an idea of justice, beauty, or goodness, for example, Plato argues that these are not constructs invented by human beings or human societies. Rather, they are fundamental aspects of reality that come down to us from that still higher reality represented by the Forms.
As far as the Allegory of the Cave is concerned, the allegory itself is an illustration of Plato's model of reality. Thus, Plato gives an image of prisoners chained together in a cave. They are trapped, with their faces pointed toward the wall. A fire burns behind them (providing light). Meanwhile, there are others in the cave with puppets, projecting shadows for them to see. For the prisoners, these shadows are reality as they experience it.
From here, Plato imagines one of the prisoners becoming free, ascending upward from the cave, and emerging into the light of the sun. For Plato, this upward journey out from the cave and into the world illustrates the relationship between human existence and that still higher reality of the Forms. Compared to the realm of abstract universal truths, our own material existence amounts to only shadows.
The forms/ideals were what the man in the cave "looked" at when he came out - the sun. This allegory is an education, a paidia, in the way to the ideal.