It's often used to praise someone for their accuracy or insight, like:
* "You hit the nail on the head when you said the problem was lack of communication."
* "He hit the nail on the head with his analysis of the market trends."
The idiom originates from the act of hammering a nail straight into wood. If you hit the nail directly on the head, it goes in easily and securely. If you miss, the nail bends or goes in crookedly.