The Fourth Doctor returns to Gallifrey after meeting a group of aliens in space, bringing Leela and K-9 with him. He is behaving very strangely and when the Chancellory Guard under their Commander, Andred, arrive at the Panopticon Chamber to interrogate him, the Doctor demands to be taken to Chancellor Borusa, who is now in charge of the Time Lords. The Doctor claims the vacant Presidency of Gallifrey having previously been a candidate and, after the demise of Chancellor Goth, is now automatically elected. Under law this request cannot be refused. The Doctor then chooses a Presidential chamber and asks it be decorated with lead lining throughout. Shortly afterward a ceremony is held to swear him in as President of Gallifrey and he is presented with the various trappings of office. However, when the circlet connecting him to the Matrix, repository of all Time Lord knowledge, is placed on his head, the Doctor collapses in pain.
The Doctor is taken to the Chancellory to rest and recover. When he regains consciousness he reminds the Time Lords that no aliens are allowed on Gallifrey and instructs that Leela be expelled from the Capitol Citadel, where she will have to fend in the wastelands. She tries to avoid banishment, but the Doctor is serious about this banishment. The Doctor now retreats to the TARDIS where he shares a secret plan with K9, but is obviously very concerned about the situation he has found himself in. He is planning to aid an invasion of Gallifrey itself and to this end sets about destroying the transduction barrier that defends the planet from external threat. K9 sets about this task while the Doctor returns to the Panopticon, the great hall of the Time Lords, and laughs cruelly as three alien beings start to materialise.
The invading beings are known as Vardans. They appear as shimmering manifestations who made an alliance with the Doctor some time ago, and the Doctor advises the Time Lords, including the stubborn Borusa, to submit to their new and powerful masters. The Doctor then asks Borusa to meet him in his office, and when this happens the Doctor explains he has had the lead walls installed to prevent the Vardans entering the room on thought waves and reading his mind. He sent Leela away to protect her, he explains, and is now able to work with Borusa to defeat the Vardan threat. A new problem has emerged, however, with the ascendancy of the obsequious and compliant Castellan Kelner, who is being far too co-operative with the Vardan occupation. The toadying yet ambitious Castellan soon has Borusa placed under house arrest and starts a process of expelling trouble-making Time Lords from the safety of the Capitol.
Leela has meanwhile kept her faith in the Doctor and reasons that if he wishes her to leave the Capitol it is with good reason, so she departs for the wastelands. She is accompanied by Rodan, a Time Lady who previously maintained the transduction barrier. They are welcomed warily by a tribe of outsiders who have rejected Time Lord society and live in the wastelands. Their leader, Nesbin, explains some of the background to his tribe. Back in the Capitol, however, things are looking grim for the Doctor when Andred corners him and decides to execute him in the name of liberty.
K9 helps the Doctor overpower Andred, and then explains the danger and abilities of the Vardans to Andred, with his TARDIS providing a shield to his thoughts. The Doctor is hoping to persuade the Vardans to reveal their true form so that he can time loop their planet. Leela has also organised her own resistance movement in the wastelands, comprising Nesbin’s people and the exiled Time Lords, all of whom are drilled into a fighting force which soon launches an assault on the Capitol.
The aliens and Kelner have meanwhile decided the Doctor is behaving in an untrustworthy manner. The Doctor reaffirms his loyalty to them by agreeing to dismantle the final force field protecting Gallifrey from attack. He does not fully disable it, but rather places a large hole in it. The Vardans use the hole to properly invade Gallifrey and appear as humanoid warriors. Their manifestation enables K9 to track down their home planet and supply the Doctor with the correct co-ordinates. He uses this to beam the Vardans back to their home world and then traps it in a time loop. At about the same time Leela and her warriors reach the Panopticon, but celebrations are short-lived when a Sontaran warrior appears in the chamber.
Gallifrey has now been invaded by the Sontarans, led by Commander Stor, who finds Kelner ever ready to pledge support, even if the other Time Lords remain resistant. The Doctor and his party escape and the Doctor uses his freedom to try and pressure Borusa into revealing to him the location of the Great Key of Rassilon, a missing item of the Presidential regalia. They then regroup at the TARDIS where Rodan is put to work using the TARDIS’ controls to repair the hole in the forcefield. However, Kelner imperils their resistance when he manipulates the stabiliser banks of the Doctor’s TARDIS to try and destroy the resistance force within by hurling them to the heart of a Black Star.
The Doctor manages to override the threat, so their enemies change tack. The Sontarans, assisted by Castellan Kelner, gain access to the Doctor's TARDIS and try to hunt down the President and his friends, pursuing them through the labyrinthine corridors. Stor is after the Great Key too, knowing the Doctor has now persuaded Borusa to yield it to him. The Doctor uses distractions to buy time while Leela and the outsiders kill the remaining Sontaran troopers. On the Doctor’s instruction, a hypnotised Rodan and K9 construct a special forbidden Time Lord weapon: the Demat Gun. Powered by the Great Key itself, the Demat Gun erases its victims from time itself. The Doctor takes the Gun and confronts Stor in the Panopticon. Stor intends to destroy the Eye of Harmony with a bomb, but the blast is cancelled out by the Doctor with the Demat Gun which obliterates Stor, wipes the Doctor’s mind of recent events, and also destroys itself. Kelner is arrested and Borusa begins the process of rebuilding Gallifrey.
The Doctor is ready to leave, but Leela decides to stay on Gallifrey because she has fallen in love with Commander Andred, leader of the Chancellory Guards. K-9 decides to stay behind to look after Leela, and they both watch as the TARDIS dematerializes. Inside the TARDIS the Doctor pushes out a box labelled "K-9 MII." He then grins mischievously into the camera, breaking the fourth wall.