They come bundled with many new lenses but they’re more often than not simply left in the box. So what do lens hoods do? And why would you need to use one?

Lens hoods have a handful of purposes although their main one is to block extraneous light from reaching the lens. This light can be used creatively to encourage certain optical aberrations to form but, more often than not, its effects are undesirable.

Flare appears as small spots in images, and it’s particularly noticeable on flat areas with little detail such as in the sky of this image. It usually resembles the shape of the lens’ diaphragm.
Extraneous light can easily reflect off internal elements in the lens, or the surfaces of cheaper filters, and this can cause aberrations such as flare. The anti-reflective coatings applied to the elements inside modern lenses help to counter this, but they are more effective for incident light rather than that entering the lens at an angle. By blocking extraneous light, contrast and saturation in images can be maintained and the scene is not obstructed by any aberrations.

Not using a lens hood has caused contrast and saturation to drop in this image.
Lens hood have two further advantages, both of which concern protection. First, should a lens be accidentally dropped while mounted on a camera, a lens hood will provide the filter thread and front element with some additional protection (clearly it’s cheaper to replace a lens hood than a lens). Second, when shooting in inclement conditions, a lens hood may prevent rain or snow from landing on the front element. The degree to which this is the case depends on the lens hood, with larger, longer types providing more protection than those only a few inches deep.
Lens hoods are usually provided as standard with most new lenses, but they find themselves most useful with wideangle types. This is because the wider-than-standard angle of view makes it easier to include light coming from just outside the frame. This will typically be sunlight but it applies to other light sources, particularly those which are harsh and directional. Some particularly wide lenses even have lens hoods built into them, which highlights how important they are at such focal lengths.

Some lenses, such as the Canon EF 11-24mm f4L USM, have non-removable lens hoods built into them.
Lens hoods are usually made from a matt plastic but they can also be constructed from metal or rubber. While metal ones have the advantage of being sturdier than the other types, rubber ones have the ability to fold down when not in use. In any case, the inside should be flocked in some way to prevent reflections.
Lens hood also come in different shapes, with petal and round types being the most common. Petal-shaped hoods are designed to match the aspect ratio of the camera’s sensor, with longer sides designed to sit along the horizontal edges and the shorter ones against the verticals. Whichever you use, it’s important to make sure the hood is designed to be used with your camera, as otherwise it may either not mount the lens or may not provide adequate protection.

Regardless of the material or design of your lens hood, you should ensure that it’s compatible with your specific lens.
In the case of petal-shaped hood, it’s just as important to position these correctly as an incorrectly mounted lens hood stands to not only be ineffective but also a potential cause of vignetting, where the edges and/or corners of the frame show a slight darkening. You should be able to tell when it’s only properly as it should click into place and prevent you from rotating it any further (if you can do this but only with force, it’s likely to be mounted incorrectly). There may also be markings on the lens which should assist you with this.
Do you need to use a lens hood? Some photographers find them to be a burden and leave them in the box while others keep them on their lens at all times. Perhaps what makes the most sense is to use it in types of situations in which you can clearly tell it will benefit image quality. So, outdoors in harsh light, for example, or potentially when capturing long exposures, where you may catch unwanted light from streetlights or the moon.
Bear in mind that if you use a lens designed for a full-frame camera on a body with a cropped sensor, the standard lens hood will be protecting the lens at its natural focal length, and so will fail to offer the best protection for the effective focal length achieved by the combination of the two. For example, a 28mm lens designed for a full-frame sensor used on an APS-C body will give you an effective focal length of around 42mm (depending on the size of the sensor). There may be an alternative lens hood which would be more suitable when using a cropped-sensor camera, so check this.
Many lens hoods can be reverse mounted so that they stay on the lens for convenience when not required, although the lens’s usability here depends on the design of both the lens and the hood. As the majority of hoods are not mounted via the camera’s filter thread but on its outer edges, it’s also often possible to use thread-mounted filters and hoods at the same time, but this is also something to check before buying.