I know Jack has already answered this, but I wanted to see if we worked it out the same way.
There are five hats, three black, two white. Only three are used.
The rear is first to answer. He see the two hats in front but does not know which colour hat he is wearing. Therefore the two in front cannot both be white.
They could both be black, but the rear guy cannot be sure that he's wearing the third black hat.
Middle guy hears the rear guy's answer and knows that his and Front guy's hat cannot both be white.
Middle can see Front guy's hat.
If it is white, then Middle's own hat must be black, and he can answer.
If Front guy's hat is black, then Middle's could also be black, so Middle cannot answer.
Realising this, Front guy knows Middle guy's hat is black and so, therefore, is his own.
It should be noted that the natives didn't forbid the three captives to speak.
Therefore Rear guy could have said to middle guy "what colour features in the phrase 'as the ace of spades'?".
The middle guy would have said 'black' and that would have allowed him to guess his hat colour. Then they would all have gone free.