Travelling through space, the TARDIS suffers an energy drain and crash-lands on the planet Exxilon. The Doctor and Sarah Jane Smith venture outside to investigate the cause of the interference, and become separated. The Doctor is captured by the planet's inhabitants - the savage Exxilons - but escapes. Sarah is attacked by one of the creatures in the TARDIS, and flees into the night. Later, she finds a huge and pristine white City with a flashing beacon.
When daylight arrives, the Doctor is found by a party of the Marine Space Corps; they take him to their ship which, like the TARDIS, has been stranded by a power drain. They are on an expedition to mine Parrinium - a mineral abundant only on Exxilon - which can cure and give immunity to a deadly space plague. The lives of at least 10 million people depend on the expedition's obtaining the Parrinium and leaving the planet within a month. They show the Doctor some photos they have taken of the nearby City - which the Exxilons worship, sacrificing anyone who ventures too close to it. Sarah does so, and is captured and taken to the Exxilons' caves to be sacrificed by their High Priest.
A ship containing four Daleks now arrives; both the Daleks' ship and their weapons have been rendered useless by the energy drain. The Daleks claim that several of their planetary colonies are suffering from plague; thus they need Parrinium for the same reason as the humans. The Daleks, the Doctor, and the humans form an uneasy alliance to obtain Parrinium and escape Exxilon. While the allies are making their way to the humans' mining dome, the Exxilons ambush them, killing a human and a Dalek and capturing the others. The prisoners are taken to the Exxilon caves where the Doctor interrupts Sarah Jane's sacrifice; therefore, he is also condemned to death. When the dual sacrifice commences, a second party of Daleks, who have replaced their energy weapons with firearms, attack in force, killing a number of Exxilons. They then force the Exxilons and humans to mine Parrinium. The Doctor and Sarah flee into underground tunnels.
The Doctor and Sarah meet a group of subterranean, fugitive Exxilons. Their leader, Bellal, explains that the City was built by the Exxilons' ancestors, who were once capable of space travel. The ancient Exxilons built the City to be capable of maintaining, repairing, and protecting itself. However, fitting the structure with a brain proved to be a mistake, as the City no longer needed its creators. On realising this, the Exxilons had tried to destroy the City, but, instead, the City destroyed most of them; the savage surface dwellers and Bellal's group are the only survivors. Bellal's people seek to complete their ancestors' last, failed act - to destroy the City and ensure their race's survival - whereas the other Exxilons worship the City as a god. Bellal sketches some of the markings on the City wall, which the Doctor recognises from a temple in Peru. He deduces that the space-travelling Exxilons must have visited Earth and taught the primitive humans how to build. Bellal also explains that the City supports itself through underground 'roots' and the aerial beacon. The Doctor realises that the beacon must be the cause of the energy drain, and decides to go to the City and resolve the problem.
The Daleks separately come to the same conclusion, that the City's beacon must be the source of the interference, and create two timed explosives to destroy it. One Dalek supervises two humans placing the explosives, but one of the humans, Galloway, secretly keeps one bomb. Two other Daleks enter the City to investigate the superstructure, but the Doctor and Bellal have arrived first and enter the City just before them. The two parties then proceed through the City - the Daleks just behind the Doctor and Bellal - passing a series of progressive intelligence tests. The Doctor reasons that the City has arranged the tests so that only lifeforms with knowledge comparable to that of the City's creators would reach the brain at the centre, allowing the City to add the knowledge of the survivors to its databanks. On reaching the central chamber, the Doctor begins to sabotage the City's computer brain; the machine responds by creating two Exxilon-like 'antibodies' to 'neutralise' the Doctor and Bellal. The pair are saved when the Daleks enter and fight the antibodies, and the Doctor and Bellal escape as the City's sabotaged controls begin to malfunction.
When the bomb on the beacon explodes, all power is restored. The Daleks order the humans to load the Parrinium onto their ship. On leaving Exxilon, the Daleks intend to fire a plague missile onto the planet, destroying all life and making future landings impossible, so that they will have the only source of Parrinium. Their true intention for hoarding Parrinium, it transpires, is to blackmail the galactic powers to accept their demands; refusal would mean the deaths of millions. As their ship takes off, Sarah reveals that the Daleks have been tricked; they have only bags of sand while the real Parrinium is on the Earth ship, which is now ready to take off. Meanwhile Galloway has smuggled himself and his bomb aboard the Dalek ship; he detonates the bomb, destroying the Dalek ship before it fires the plague missile. Back on Exxilon, the City disintegrates and collapses, the Doctor sadly commenting that the Universe is now down to 699 Wonders.