A prism works by slowing down light proportionally to wavelength as it enters into the prism, which causes the rainbow effect. Light changes speed as it moves from the air to another substance. This causes the light to change course.
In order to achieve the effect, the light must enter the prism at an angle. The light then bends once it is inside the prism and again when the light leaves the prism. Each wavelength of light gets bent at different angles. This causes each color to fan out and become more distinct.