The occasional shake or toss of the head is normal, and is often caused by insect irritation or annoyance at the bit or rider.
Chronic head shaking is often caused by allergies or ear mites. The latter can easily be diagnosed - a horse with ear mites will also react violently when the affected ear or ears is touched, and may continue to do so for some time after the problem is cleared up.
Head shaking can also be caused by poor riding (I know one horse who shakes his head if not ridden in the way he prefers - and he's quite picky about it), tooth or mouth problems or a bit that is too harsh or uncomfortable for the horse.
In some cases, a cause for the problem can't be found. The horse may need re training or it may have some kind of nerve or light sensitivity problem that's not easily fixed.