Ignore the behavior. As long as your child is intellectually normal, ignoring the behavior is the fastest way to make it disappear.
Ignore It. Either your child is banging their head as a self soothing mechanism, which is harmless or they are having a temper tantrum, which is best totally ignored. Head banging causes no internal brain damage, although I have seen small superficial bruises. This is assuming your child is a normal healthy child in all other respects. If there are serious questions in other areas, you should consult your md.
Soothing. There are some children who bang there heads repetitively as a way of self-soothing or comforting, often before falling asleep or when upset. The best things to do are to try to remain calm, move the child to a place where he/she won't hurt himself, try some gentle distraction or "ignore" the behavior. If the behavior seems excessive, consult your pediatrician.