Suppose that the rear man sees his two friends wearing two white hats. Then he would be able to tell the color of his hat.
In this case, he answers "I don't know", so we deduce that his friends aren't wearing white hats.
Now, the middle man knows that, if the guy in front of him is white, he (the guy in the middle) cannot be white. If the 3rd guy was black, the man in the middle would indeed be able to answer.
Though, he said "I don't know", so the third guy isn't white. The middle guy has no information about his hat, as we know.
Finally, it's time for the last guy to answer. Listening to the previous answers, he can deduce that he is black and answers!
If you want a solution that analyzes the other cases one by one, here is it:
- BBB The first says "I don't know", implicitly stating that the other two aren't both white. The second man, seeing a black hat in front of him, doesn't know the color of his hat. The third guy, knowing that he can't be white, deduces his color (black).
- BBW As in the previous case, the first says "I don't know". The second, seeing a white hat, deduces that he is black and answers.
- BWB The first says "I don't know". The second doesn't know as well. As in BBB, the third knows that he must be necessarily black.
- WBB The first says "I don't know". The second doesn't know too. The third answers "black" consequently, same reason as before.
- WWB The first doesn't know. The second neither. As before, the third answers "black".
- WBW The first doesn't know. The second, seeing a white hat in front of him, answers "black".
- BWW The first sees two white hats, thus he must be black!