Not all contact to the head is a flagrant foul. There are several dimensions on which the foul is evaluated.
Intent to hurt or recklessness is the most obvious factor. Whether or not the fouler is making a play at the ball also matters, although accidental contact when missing in an attempt to swipe at the ball can still be flagrant. The harder a foul is, the more likely it is to be ruled flagrant, whether or not there is intent.
The possibility of injury also matters. A foul is more likely to be flagrant if it occurs when the fouled player is in a "vulnerable position." For example, when a player is standing and holding the ball and a defender tries to poke it away and accidentally hits him in the head, that's different from a foul hitting a player in the head while he is in the air, especially if there is a chance the contact can whip his head back or cause him to land in a fashion where he can't protect himself from the fall.
A hard foul above the neck while a player is in the air, regardless of intent, seems to be a foul that the league would like to be ruled as flagrant. This seems to be part of the concern in all sports about the effect of concussions on athletes. Even non-concussive events that involve whiplash seem to have a cumulative effect, so it would be wise to prevent incidents where an athlete has his head snapped backward or where he takes a hard fall landing on his back.
Viewed through that lens, it seems like Jermaine O'Neal committed a non-malicious flagrant foul.