Like hair spray, hair gel is made of polymers. The polymers are water soluble, and tend to have positively charged areas that are attracted to the negatively charged areas of the hair strand.
As the gel is applied, the long strands of the polymers wrap around the hair strands, and bind them together. When the gel dries, the polymers shrink around the strands of hair, pulling them tightly together.
Hair gels usually contain alcohol so they dry faster. Other ingredients include plasticizers (like the ones in hair spray) that keep the polymers soft and elastic, and usually some type of fragrance. Sunscreens are another common additive.
The main gelling agent is often a vegetable gum, such as guar gum. Other vegetable gums, such as marshmallow and aloe vera are sometimes used. For extra hold and support for elaborate hairstyles, acrylic plastic polymers (similar to floor polish and white glue) are used.
Humectants (molecules that absorb water from the air) are used to keep the gel from completely drying out and feeling crusty. The simplest ones are just sugars, such as agave nectar.