Hats are very useful things. Regularly used to keep out the cold from the most northern reaches of the human (and occasionally Time Lord) cranium, though this is potentially seen by some as unoriginal. Frequently used for (but insufficiently reported on) as foot rests, a drinking aparatus (as at least once referenced in the television programme the Simpsons), containers for moisture cream and occasionally used to demonstrate violent acts (such as punching the top off it).
Now you may be wondering what hats have to do with Doctor Who - or even what Doctor Who is (a fictional television programme about a humanoid alien called the Doctor, from the - now blown up - planet Gallifrey , who can travel in time and space in his TARDIS). In the context of Doctor Who hats have a significance that is only overshadowed by "that scarf". Like "that scarf" (the Doctor is known for wearing scarves) however, it is a common misconception (at least until recent times) that the Doctor always wears a hat - when of course he is known also to only wear hair (mostly when it is growing out of his head).
Whether the Doctor's hat has magical (or other worldly at least) powers is not made clear in the series. Though the Fifth Doctor's (played by Peter Davison) ability to roll up his hat, and be able then to wear it again must surely be a miracle in itself - either that or just good sturdy workmanship. Unlike the Doctor's pockets it is unknown whether the Doctor can fit more on the inside than would normally seem possible - though surely it can not be ruled out - he does have a big head.
The Doctor has been known to wear a number of different hats. Because the Doctor has the ability to regenerate (change his body completely when it is near to death - there have been ten incarnations of the Doctor to date), his taste in hats - as well as clothing - has varied wildly. The first Doctor (played by William Hartnell) was known on occasion to wear hats though this was the exception rather than the rule. The Second Doctor (played by Patrick Troughton) began by wearing a stove-pipe hat, which slowly dwindled in usage as his tenure went on.
The third Doctor (played by Jon Pertwee) tended not to wear a hat except in certain exceptions. The fourth Doctor (played by Tom Baker) was both well known for his hat(s) and for his overly long scarf - both of which were used as weapons and means of distraction - as demonstrated in the episode Robot.
The fifth Doctor wore a Panama hat which he was frequently rolling up and putting away, only to pull out and wear later on in the story. The sixth Doctor (played by Colin Baker - no relation to Tom) went virtually without headwear through his entire reign, with the exception of wearing a policemans helmet. The seventh Doctor (played by Sylvester McCoy) regularly wore a hat, as well as a considerably shorter scarf.
However tragically since the seventh Doctor was shot in San Fransisco in 1999, just before New year and regenerated in to his eighth body (played by Paul McGann), he has rarely been seen wearing a hat as part of his regular attire. He did once - perhaps due to safety concerns - wear a helmet in his tenth incarnation (played by David Tennant) on a trip back to the fifties whilst on a scooter. He also wore space helmets, though like in his sixth incarnation (very very briefly) and his previous safety precautions on a bike these are headwear rather than hats - which is a rich vein to be tapped by another entry in the future.
Whether or not he blamed the hat for the tragedy that caused him to regenerate, or just that he's lost interest in head wear - we may never know.
Have we seen the last of the Doctor wearing a hat? Only time will tell.