It means when people are thinking too narrowly.
E.g. if you ask a general in the army how to solve a problem, he might say, "we can use the army to fight them". He will not be thinking about other solutions, because he is very narrow-minded in his approach.
A hammer is a tool which is used only for nails. So someone who only has a hammer thinks of everything like a nail, because he is thinking it's all he has that he can use.
When all you have is a hammer ,everything looks like a nail = you look at every problem like it is a nail, because all you have is a hammer, so you're only thinking about using a hammer - when in fact there are other solutions he should think of.
For example, for a screw, you must use a screwdriver. But if that man only owns a hammer, he will think of it like a nail. But it's wrong.
get it?