A lens hood’s main purpose is to block light. If the lens is a prime lens (fixed focal length, non-zooming) the hood will resemble a tube, often larger at one end than the other. For zoom lenses the hood will have a curved opening at one end. This curve is cut to the zoom range of the lens and allows for the wider field of view afforded smaller focal lengths, while still attempting to block most light at a longer focal length. It’s a compromise that matches the compromise of a zoom lens.
The hood will help block out light that is coming into the lens and causing flare by striking the outer lens elements (the glass pieces that make up the entire lens) at a less than optimal angle. This is light that never would have made it to the sensor and isn’t needed. Instead, it causes those discolored spots you might have seen, shaped like the lens aperture (typically a hexagon or octagon). While lens flare also occurs from light coming directly into the lens, flare from off-angle light can be prevented.
A lens hood will not help you when the sun (or light source) is actually in your shot. While it can help reduce extra light from reflected objects nearby (windows, white walls, etc.), the effect is minimal.
But in reality, you should use the hood whenever you can. My reason for wording the answer this way is so you don’t freak out if you forget your hood or it is hard to pack (many wide angle lens hoods don’t always fit in the holes in camera bags). If you happen to be missing your hood for the day, simply use your hand, a book or any other likely object to block out the flare from the main light source, while making sure you don’t get the shading device into the picture. This won’t block the reflected light, which can minutely soften a photo (not to a point most of us will ever realize, mind you) but it will help take care of the main flare issue.