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 (but insufficiently reported on) as foot rests, a drinking apparatus, (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 television programme about a fictional humanoid alien called the Doctor, from the — now blown up — planet Gallifrey, who can travel through time and space in his TARDIS1). 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 also known 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 ability to roll up his hat and wear it again later must surely be a miracle in itself — either that or it's 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 cannot 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 well known both 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 (played by Peter Davison) 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 policeman's helmet, very, very briefly. The seventh Doctor (played by Sylvester McCoy) regularly wore a hat, as well as a considerably shorter scarf.
Tragically, since the seventh Doctor was shot in San Francisco just before New Year in 1999, and regenerated into his eighth body (played by Paul McGann), he has rarely been seen wearing a hat as part of his regular attire. Whether or not he blamed the hat for the tragedy that caused him to regenerate, or just that he's lost interest in headwear, we may never know. He did once, perhaps for health and safety reasons, wear a crash helmet in his tenth incarnation (played by David Tennant) whilst riding a scooter during a trip back to the 1950s. Tennant also wore space helmets, and together with his crash helmet it should be noted that these are safety headgear rather than hats. This is a rich vein which could be tapped by another Entry in the future.
Have we seen the last of the Doctor wearing a hat? Only time will tell.