It has to be perfectly in your strike zone. The perfect spot is when you hit it and the little starts appear and it makes a really sharp sound.
On most pokemon, that isn't actually at the top of their head. It's located somewhere farther down, normally right below the neck.
It takes practice and a little bit of luck.