A hat was an article of clothing worn on the top of the head.
The Doctor's hats
In the majority of his lifetimes, the Doctor wore a hat on a regular or semi-regular basis. The incarnations that showed no particular interest in hats were the Sixth, War, Ninth, Tenth and Twelfth Doctors.
The First Doctor's usual hat of choice was a karakul or Astrakhan, a triangular hat often associated with Afghanistan, or what one account called an "oddly shaped fur hat". (PROSE: Doctor Who and an Unearthly Child) It was this hat he was wearing when he met Barbara Wright and Ian Chesterton, as well as during his first and at the start and end of his last adventures with Ben Jackson and Polly Wright. (TV: An Unearthly Child, The War Machines, The Tenth Planet) However, he also possessed a kind of Panama hat, which he wore in hotter climes. He donned a white Panama hat when he was on the hunt for the Daleks' taranium in Egypt and while sunbathing on the planet Aridius. (TV: "Escape Switch", TV: The Chase)
This incarnation was not averse to occasionally playing dress-up. When in Revolutionary France, he obtained period-appropriate wardrobe. Because the costume was that of a regional official, his hat was particularly elaborate, bearing a strong resemblance to an early 18th century French army officer's hat. (TV: "The Reign of Terror")
Early on, the Second Doctor was particularly obsessed with hats. One of his very first acts after regeneration was to rummage through a trunk in the TARDIS to find a stovepipe hat to wear. He wore the hat throughout his first adventure on Vulcan. (TV: The Power of the Daleks)
He sometimes uttered the phrase, "I would like a hat like that (or this)" if he saw a hat he particularly fancied. (TV: The Power of the Daleks, The Highlanders) In fact, when he arrived in Scotland in the 18th century, he happened across a Balmoral bonnet on a field of battle and immediately donned it. Later in the same adventure, he wore a British Army tricorn as a disguise.
On a trip to Atlantis, he briefly returned to the stovepipe but continued to show his penchant for disguise by temporarily donning a gypsy headdress to appear as a merchant. It was here, too, that Polly noted his fondness for hats. When he began enthusiastically eating food made of plankton, the Doctor's praise was effusive. When Ben wondered why the Doctor was so excited, Polly replied, "I don't know. I've never seen him go for food like this before. It's usually hats." (TV: The Underwater Menace)
Like his predecessor, the Third Doctor almost immediately donned a hat after his regeneration. He found a trilby amongst a physician's clothes at the Ashbridge Cottage Hospital. When he tried it on and looked in a mirror, he seemed to give it a look of approval. However, he wore it for only a short time and generally was without a hat. (TV: Spearhead from Space onwards)
Just like his previous selves, the Fourth Doctor's initial clothing instincts were to reach for a hat after his regeneration. His outfit was supplied entirely from the TARDIS wardrobe and he tried on a variety of outfits, most of them with hats. Amongst the rejected hats were a Viking helmet and a tall clown's hat. Finally he settled on a wide-brimmed brown fedora. (TV: Robot) Unlike in his two previous incarnations however, he used this hat fairly consistently for a period of his life, before replacing it with green felt fedora. (TV: The Brain of Morbius) Over time, he began to use the two hats alternately, until he replaced them both with a burgundy felt fedora. (TV: The Leisure Hive)
On occasion, he substituted his regular hat with another, such as when he donned a tam o' shanter whilst in Scotland, (TV: Terror of the Zygons) a sombrero when he thought he was arriving in Benidorm, Spain, (COMIC: Doctor Who and the Star Beast) a deerstalker while visiting Victorian London to go along with his Sherlock Holmes-styled outfit, (TV: The Talons of Weng-Chiang) and a bowler hat whilst investigating Fang Rock lighthouse. (TV: Horror of Fang Rock)
Like his previous incarnation, the Fifth Doctor had a regular hat. From his first adventure, he carried with him a roll-up Panama hat with a hatband of the same colours as the rest of his outfit. (TV: Castrovalva) He tended to wear it upon first exiting the TARDIS in a new location, but often quickly rolled it up and put it back in his jacket.
Like the Fifth Doctor, the Seventh Doctor also wore a Panama hat, though it was of a different design, with a red Paisley hatband. He wore it regularly, even when indoors. He initially wore an extensively battered hat, (TV: Time and the Rani - Dragonfire) but replaced it with a much less damaged example prior to his encounter with the Daleks in 1963. (TV: Remembrance of the Daleks) He typically removed it from his head to greet others or in respect of someone recently deceased. (TV: Time and the Rani - Survival) When he took to wearing a darker jacket, he also changed his hatband into a subtly darker one. (TV: Battlefield)
After his regeneration in San Francisco on 31 December 1999, the amnesiac Eighth Doctor ransacked the lockers of the hospital staff for a new outfit. He decided against the stetson that went with the "Wild Bill Hickok" costume which he chose to steal. (TV: Doctor Who)
The Tenth Doctor showed little interest in hats. He did once, near the end of his life, briefly sport a fez that fell out of a time fissure, before it was snatched off of his head by his future self. (TV: The Day of the Doctor)
Soon afterwards, he also wore a straw hat, along with sunglasses and a lei, implying he had just finished a relaxing time at a beach. (TV: The End of Time) He also donned a sombrero on the beaches of Centuria. (COMIC: The Power of the Cybermen)
The Eleventh Doctor wore a variety of hats, seeking to acquire unique ones. Much like he did during his second incarnation, he possessed a rather obsessive desire to wear a hat; he would get distracted by a hat he liked even while running away from enemies. (TV: The Big Bang, The Curse of the Black Spot) He considered damaging a hat to be offensive. (TV: A Town Called Mercy) Unfortunately for him, River Song did not share his taste in hats, at least twice shooting them. (TV: The Big Bang, The Impossible Astronaut)
While in the National Museum, he found a fez of which he became instantly fond. The fez was later removed by Amy Pond and destroyed by River Song. Undeterred, he noted to himself that it would be easy to buy another one. (TV: The Big Bang) He wore another fez while acting as an extra in a film which Rory and Amy were watching, (TV: The Impossible Astronaut) and even acquired one previously owned by his old pal Albert Einstein. (TV: Death Is the Only Answer) He later wore one on a Christmas trip with Kazran Sardick and Abigail Pettigrew to Egypt (TV: A Christmas Carol) and had one in 2013 London. (TV: The Bells of Saint John) When he and Clara Oswald investigated the National Gallery, he spotted a fez inside a display case and immediately put it on. When Clara noticed, she commented that one day, he was going to be able to just walk past a fez. The Doctor declared that was never going to happen. He later used it to test the safety of a time fissure. (TV: The Day of the Doctor)
The Doctor also showed a fondness for stetsons, being given one by Craig Owens as a gift for his trip to Utah. (TV: Closing Time) He wore his gift during his investigation to find out why the Silence wanted him dead. The Teselecta copied the stetson along with his form while impersonating him. The copied Stetson was shot off the copy's head by River. (TV: The Impossible Astronaut/The Wedding of River Song) On another occasion, the Doctor borrowed a stetson that had been damaged by a shot fired by the Gunslinger. (TV: A Town Called Mercy)
This version of the Doctor also owned a top hat, which he wore at Amy and Rory's wedding, (TV: The Big Bang) after having been poisoned by River in Nazi Germany (TV: Let's Kill Hitler) and again to conceal a device that allowed him to lure the Squall back into their dimension of origin. (PROSE: Paradox Lost) He also wore one as part of his Victorian Era clothing while brooding over the loss of Amy and Rory. (TV: The Snowmen)
While posing as Sherlock Holmes, he also wore a deerstalker hat similar to the one his fourth incarnation had during a previous visit to Victorian London. (TV: The Snowmen) Later, the Doctor wore a bowler hat. (TV: The Crimson Horror)
The Twelfth Doctor did not usually wear a hat, but at Clara's insistence donned a fedora when visiting Las Vegas in the 1960s. (COMIC: Gangland) He also wore a top hat when visiting the Frost fairs with Bill Potts. (TV: Thin Ice)
The companions' hats
Those companions attached to UNIT or other armed forces wore hats as a part of their uniforms. Ben Jackson's sailor's cap, for instance, identified him as a member of the crew of the HMS Teazer. UNIT officers, like the Brigadier and Captain Mike Yates, most often wore a rigid, green-and-red officer's cap, similar to the British Army. Surgeon-Lieutenant Harry Sullivan's black, white and gold hat — otherwise quite similar to the standard Army issue — betrayed his secondment from the Royal Navy. Sergeant John Benton most often wore a beret in the field. Indeed, UNIT soldiers and officers alike wore berets in the field. Over the years, the standard-issue UNIT field beret changed from chocolate brown (TV: The Ambassadors of Death) to khaki green (TV: Terror of the Autons) to United Nations blue (TV: Battlefield) to red. (TV: Aliens of London, Planet of the Dead)
Although not in the military, a couple of companions nevertheless had headwear that was a part of a uniform. Tegan Jovanka had a triangular cloth cap as a part of her stewardess' uniform, but she largely abandoned its use after meeting the Fourth Doctor and Adric. (TV: Logopolis) Likewise, while Nyssa of Traken was "in uniform" as a Trakenite royal, she wore a tiara. (TV: The Keeper of Traken)
These exceptions aside, most companions simply didn't wear hats on anything like a regular basis. A few wore the occasional hat strictly as a fashion accessory, often whimsical.
In both her incarnations, Romana was fond of wearing hats. Romana I wore a flat cap on her first visit to Earth (TV: The Stones of Blood) and an elaborate purple-and-green hat that was "what everyone . . . [was] wearing" on Tara. (TV: The Androids of Tara) Romana II briefly toyed with wearing the Fourth Doctor's fedora after her regeneration. She later wore a boater when punting in Cambridge (TV: The Five Doctors) and a straw sun hat whilst on a beach. (TV: The Leisure Hive)
Liz Shaw generally didn't go in for hats, but did once wear a white, broad-brimmed hat to drive Bessie. (TV: The Ambassadors of Death) Likewise, Sarah Jane Smith was mostly hat-less but did wear a casual spring hat whilst investigating the National Institute for Advanced Scientific Research. (TV: Robot)
Behind the scenes
In the video game The Eternity Clock, the Doctor's collectable items are various hats (including the fez, top hat and Stetson).