The Ultimate Foe is the fourth and final serial of the larger narrative known as The Trial of a Time Lord which encompasses the whole of the 23rd season of the British science fiction television series Doctor Who. It was first broadcast in two weekly parts on BBC1 on 29 November and 6 December 1986. This segment is also cited in some reference works under its working title of Time Incorporated (or Time Inc.). The title The Ultimate Foe is never used on-screen and was first used in relation to these episodes for the 1988 novelisation, with the two episodes that comprise the serial being referred to as The Trial of a Time Lord Parts Thirteen and Fourteen. This was the last story to feature Colin Baker as the Sixth Doctor, as Baker declined to do the regeneration (when he was dismissed from the role) for the following story, Time and the Rani.
In the serial, continuing from the events of Terror of the Vervoids, the genocide charges against the alien time traveller the Sixth Doctor for destroying the Vervoids are interrupted by the Doctor's enemy the Master (Anthony Ainley), who seeks to have the Doctor and his prosecutor the Valeyard (Michael Jayston) destroy one another in the virtual micro-universe the Matrix, and to create a power vacuum where the High Council of the Time Lords is deposed so the Master can take command.
Plot
The Sixth Doctor boldly claims the Valeyard's evidence has been falsified, and the Matrix has been tampered with. The Keeper of the Matrix insists this is impossible. Glitz and Mel arrive unexpectedly in the courtroom. The Master appears on the Matrix screen to claim responsibility and to demonstrate it's possible to breach the Matrix. At the Master's insistence, Glitz reveals the data he tried to obtain on Ravolox included technological secrets from the Matrix, which was stolen by the Sleepers. The Time Lords traced the Sleepers to their base on Earth and dragged the planet across space to the location in which the Doctor found it, nearly annihilating all life on the planet in the process. The Doctor denounces the Time Lords as decadent and corrupt. The Master explains that the Valeyard is a manifestation of the Doctor's darker side "somewhere between [the Doctor's] twelfth and final incarnation"; the High Council offered the Valeyard the Doctor's remaining regenerations in exchange for falsifying evidence.
When the Doctor demands to halt the trial as he cannot be both the defendant and prosecutor, the Valeyard flees into the Matrix, a virtual reality where normal logic does not apply. The Doctor pursues with Glitz, emerging next to a building labelled "The Fantasy Factory (proprietor: J. J. Chambers)". A clerk named Mr. Popplewick sends them to a deserted wasteland. To the Doctor's horror, hands emerge from the ground and grab him, dragging him underground. Glitz is unable to rescue him, but the Doctor rises from the ground unharmed, insisting correctly that nothing that happens in the Matrix is real. The Valeyard appears and taunts the Doctor before unleashing nerve gas, forcing the Doctor and Glitz to take refuge in a run-down cottage. As they stumble inside, it dematerialises – it is the Master's TARDIS.
The Master reveals that he wishes the Doctor to prevail over the Valeyard, since he fears the Valeyard's ability to defeat him. He puts the Doctor into a catatonic state and sends him out of his TARDIS to lure the Valeyard out of hiding. The Valeyard emerges onto a balcony, but fires upon the Master, forcing him to flee. Mel emerges from a tunnel and the Doctor, recognising her voice, emerges from his trance. She leads him out of the Matrix and into the trial room. They agree that she should tell the truth, and she confirms to the court that the scenes of the Vervoids' destruction, the basis of the Valeyard's charge of genocide, are as she witnessed them. The Inquisitor finds the Doctor guilty and declares that his life is forfeit. He accepts the verdict as the fulfilment of justice and is led off to execution.
However, this is another illusion. Mel is frantic that the Doctor needs help, grabbing the Keeper's key and entering the Matrix. She finds the Doctor and warns him – but he had already realised the courtroom was a fake and merely wished to reach a final confrontation with the Valeyard. Bribed by the Master, Glitz returns to the Fantasy Factory; he finds the master tape of the data he thought was destroyed on Ravolox. Glitz escapes with the data to the Master's TARDIS, while the Doctor asks Popplewick for Chambers; Popplewick doesn't comply. The Doctor and Mel lay hold of him, and the Doctor peels away his face to reveal Popplewick as a disguised Valeyard. They realise that a concealed machine in the room is a particle disseminator, with which the Valeyard plans to murder the members of the court.
The Inquisitor learns the High Council has been deposed. The Master appears on the Matrix screen to offer to impose order in return for power. He loads Glitz's master tape into his TARDIS systems, but a booby-trap is triggered, paralysing him and Glitz. Mel emerges from the Matrix to warn the Time Lords. They cannot turn off the Matrix screen, but the Doctor sabotages the Valeyard's weapon and the Fantasy Factory explodes; he flees the Matrix, back to the courtroom.
The Inquisitor drops the charges against him and reveals that Peri survived the events on Thoros Beta and became Yrcanos's queen. She urges the Doctor to stand for Lord President of the new Council, but he suggests she should stand. He urges the Time Lords to be lenient towards Glitz, while he returns Mel back to her proper time.
As the Inquisitor leaves the trial room, she gives instructions to the Keeper of the Matrix. As he looks up at the camera, he is revealed to be the Valeyard.
