If you count "The Trial of a Time Lord" (1986) as a single serial (which is how it was broadcast), it is the longest -- 14 episodes. If you count it as 4 linked serials (as many fans do), the longest would be "The Daleks' Master Plan" (1965-66) -- 12 episodes. "Master Plan" also had a 1-episode "prequel" called "Mission to the Unknown", which is considered part of the storyline in its Target Books novelisation. Two Jon Pertwee stories "Frontier in Space" and "Planet of the Daleks" were also commissioned as a single 12-episode narrative, which ties the length of "Master Plan" if you don't count "Mission".)
In terms of total time, the Torchwood season-long arc "Miracle Day" (2011) covered 10 one-hour episodes, or the equivalent of 20 episodes of the original series. If you don't count Torchwood or other spinoffs, the 14 half-hour episodes of Trial or 12 of Master Plan are the longest by time. However, the 1978-1979 season of Doctor Who consisted of six interconnected serials telling the story of the Doctor's quest to reassemble the The Key to Time. If considered one big story, its 26 episodes are the all-time record-holder by any measure.
If other media are included, then the 21-part comic story "A Rose by Any Other Name" (comic story) would be the longest.