On board a space station orbiting Earth, something opens up a compartment containing a sleeping human and enters it. Years later, and leading on from the end of Robot, the TARDIS materialises in a darkened room on board the station, and the Fourth Doctor, Sarah Jane Smith and Harry Sullivan emerge. As Harry apologises, the Doctor complains about Harry's turning the ship's helmic regulator too far, which is what made the TARDIS travel randomly to begin with. The sealed room also does not have much air or heat in it and the travellers find it hard to breathe. The Doctor manages to turn the lights on, revealing instrumentation on the walls. Harry curiously presses a button that opens an adjacent chamber into which Sarah wanders while the Doctor and Harry's attentions are focused on the controls, only to be trapped as the door slides shut.
The Doctor estimates the construction of the controls dates to the 30th Century, but that the station has been here for several thousand years. As the air grows thinner, Harry and the Doctor notice Sarah is missing and manage to open the door to the control room again. Sarah and Harry grow weaker and the Doctor discovers the life support circuits have been bitten through. He manages to reconnect it with his sonic screwdriver and the rest of the power and oxygen comes up. Harry and the Doctor lay the recovering Sarah on a nearby couch and go exploring, only to run into the reactivated defence system that nearly electrocutes them. While Harry and the Doctor manage to distract the Autoguard and trigger the shut-off control, Sarah is teleported away by the couch to another part of the station.
Lying half-sedated in the receiving chamber of the matter transmitter, Sarah hears a pre-recorded voice telling her to remain still and prepare for the "final phase" of her processing. Another voice, that of the High Minister, praises her for making the supreme sacrifice that will preserve all their pasts. Cryonic gas flows over Sarah as she falls asleep. Harry and the Doctor find Sarah missing, and go in search of her. Harry spots something moving in the corridor and they discover a slime trail on the floor, like that left by a giant gastropod. Passing through a decontamination chamber, the two find the room where Sarah was, containing cryogenically preserved botanical and animal specimens and a wall of containers holding microfilm with the sum total of human knowledge. Harry suggests that the station is a sort of lifeboat, and the Doctor agrees. Some cataclysm must have occurred on Earth, and this Ark is humanity's response.
Another panel slides open, and beyond that are chambers full of human bodies contained in individual compartments and in suspended animation. The Doctor soliloquises on the indomitability and tenacity of the human species to survive any catastrophe and being willing and ready to outsit eternity. They then find a slime trail similar to the other one emerging from a vent. Exploring further, they find Sarah in one of the cryogenic pallets, deep in suspension. Harry searches for a resuscitation unit, but as he opens a storage cupboard, a huge insect-like alien nearly falls on him...
The Doctor notes that the alien insect has been dead for a long time and is almost mummified. Just then, one of the pallets activates and a woman revives from suspended animation. She introduces herself as Vira, a senior medical officer, and demands to know who the Doctor and Harry are. When they explain what has happened to Sarah, Vira injects her with an agent that will revive her. While that runs its course, she does the same to the Ark's leader, Lazar, nicknamed "Noah". In answer to Vira's questions, Harry says he is from Earth, but Vira says that is impossible as solar flares destroyed all life and it would have taken 5,000 years for the biosphere to be viable again. The Doctor tells Vira that the Ark's inhabitants have overslept by several millennia, thanks to the insect visitor that sabotaged the control systems.
In another part of the station, the larva-like creature Harry saw earlier tampers with a hatch leading to the solar stack which supplies power to the Ark. This interrupts the power to the revivification process. The Doctor turns on the secondary supply from the control room and goes to check the main stack as Noah and Sarah finally awaken. Noah is aggressive at the sight of Harry and Sarah, concerned that "regressives" could contaminate the Ark's carefully balanced genetic pool, but Vira tries to assure him the travellers are harmless. Noah goes in search of the Doctor. In the meantime, the Doctor reaches the power room and sees, through an observation port, something growing inside the stack. He then proceeds to the control room.
Noah finds the Doctor there trying to deactivate the stack to prevent the creature inside from absorbing more energy. Before the Doctor can convince him of the necessity of preventing the creature's growth, Noah shoots him with a stun gun. Meanwhile, Vira finds that technician Dune's body is missing, and when informed, Noah is convinced that the TARDIS crew are responsible. When Noah investigates the power room himself, the creature there brushes his hand with slime and he collapses. The Doctor awakens, and rushes to find Noah but meets him in the corridor instead. Noah accuses him of sabotage, informing the Doctor that the observation port was broken. The Doctor realises that, whatever the creature was, it has escaped.
Returning to the cryogenic chamber, they find Vira has revived Libri, who at first reacts to Noah with horror, claiming he saw a shape. Noah asks Libri to guard the travellers while he goes to shut down the revivification. When Vira questions him as to why, Noah seems to be struggling with some internal conflict. Noah then claims that he is Dune, or that Dune is "inside" him, before he rushes off. Vira is worried about Noah's outburst and concerned that something might have gone wrong with his revival, as stopping the revivification now could be damaging to the sleepers.
The Doctor suggests that Noah's mind is no longer his own — the alien consciousness was what Libri was reacting to — and convinces Libri to go and stop Noah. While examining Dune's empty pallet, the Doctor finds part of an egg membrane. Closer examination of the insect corpse shows that its egg tube is empty — it had laid its eggs in Dune and the larvae digested his body and knowledge of the Ark's power systems. Libri confronts Noah, but is unable to shoot his commander, and Noah kills him instead. Noah's hand is covered with a green growth where the larva had touched him before. He is now slowly mutating into something else...
The High Minister's voice echoes through the Ark, identifying it as Space Station Nerva, praising the passengers for their efforts and encouraging them to rebuild human civilisation. Noah manages to fight off some of the alien influence and orders Vira via the communications system to expedite the revivification and get everyone off the station before the insect aliens (called the Wirrn), absorb them and take over the Earth. The Doctor and Vira go to find Noah while Harry and Sarah revive the rest of the technical crew.
When they find Noah, the growth has taken most of the left side of his body over. The Doctor tries to get Noah to tell him how long they have before the Wirrn reach maturity, but he runs away. Vira, sadly, tells the Doctor that Noah and her were pair-bonded for their new life on Earth. Lycet and Rogin, the only two left of the technical crew, are awakened and shocked to hear what has transpired since their suspension. The Doctor tells Harry to dissect the Wirrn corpse to see if they can find a weakness. Vira wants to carry out Noah's last orders and continue the revivification, but the Doctor tells her it would take too long, as the Wirrn will have matured from the larval into their imago stage before then. He proposes that they can destroy the Wirrn while they are in their dormant, pupal stage and Vira agrees.
The dissection of the Wirrn corpse reveals a lung structure than can recycle carbon dioxide back into oxygen. This self-contained system suggests the Wirrn live in space and only need to occasionally touch down on planets for food and oxygen. The Doctor tries to hook a neural cortex amplifier and a video screen to a sample of Wirrn skin in order to gain access to its latent memories. When the signal proves too weak, the Doctor attempts to stimulate it by running it through his own cerebral cortex, despite the others' protests.
The larva breaks through into the adjacent cryogenic chamber, killing Lycet. The others seal the door, but cannot interrupt the Doctor's procedure in case the shock kills him. On the screen, they see the memories of the dead Wirrn (revealed to be a Queen), how she came to the Ark and was fatally wounded by the Autoguard. However, the Queen still managed to cut the power circuits and lay her eggs inside Dune before dying.
Rogin and Harry get fission guns from the armoury, encountering Noah, who is now nearly transformed, covered in the green growth. They fire at him, managing to drive him back, and return to the specimen room where the others are. As the larva tries to break through, they fire sustained bursts at it, finally making it retreat back into the vents. The Doctor asks Rogin if they can electrify the infrastructure, electricity being the Wirrn's weakness. Rogin says it can be done from the control room, but Noah is roaming the corridors. The Doctor reverses the transmat chamber in the specimen room to send the others back to the control room, but only manages to sent Rogin and Harry before the power fails. As the Wirrn do not need oxygen in their pupal stage, it was safe for them to shut off the systems to suffocate the humans. Knowing that the Wirrn are now at least dormant, the Doctor goes to the power room, where he finds Wirrn pupae. As he tries to reactivate the solar stack, a now fully-transformed Noah attacks him...
Fortunately, Vira and Sarah have followed the Doctor. Vira shoots at Noah, allowing the Doctor to reach them. As they back out of the room, Noah tells Vira to leave the station in the transport ship, or else the Wirrn will hunt them down when they emerge. Noah explains that a thousand years ago, the Wirrn were driven from their home, Andromeda, by human space settlers. Since then, they have drifted through space, looking for a new world, and have now claimed the Ark for their own. While the Wirrn can live in space, their breeding colonies are terrestrial, and they need humans as hosts. They intend to absorb all human knowledge, achieving revenge for the loss of their original home and becoming an advanced technological species within a single generation.
Behind Noah, the pupae begin to crack open, and the Doctor and the two humans retreat to the control room. They have to somehow electrify the cryogenic chamber to stop the Wirrn from feeding on the sleepers, but the Wirrn control the solar stack. Sarah suggests using the transport ship, which must have its own power generators. The ship is close by, but they will have to run the cable through conduits or else the Wirrn will simply cut it. The conduits are narrow, and without a mechanical cable runner, only Sarah is small enough to fit through them.
As Sarah navigates through the conduits guided by Rogin through a two-way radio, the Doctor hooks up the cables in preparation for Sarah as the Wirrn emerge and wander the Ark. Sarah gets stuck just metres from the Doctor, and almost gives up, until the Doctor goads her into crawling the last of it by calling her useless. Connecting the last cable, the Doctor gives the word to Rogin in the ship and the walls of the chamber electrify, driving the Wirrn back. One nearly gets through a broken vent, but the Doctor manages to force it away with a live cable.
Noah turns the Ark's power back on and, as the Swarm Leader, offers the others safe passage from the Ark if they surrender, leaving the sleepers for the Wirrn. If they do not, the Wirrn will shut down the oxygen pumps. The Doctor tries to appeal to what is left of the human in Noah with reminders of what Earth is like, asking him to lead the swarm into space where they belong, but Noah claims he has no memory of Earth. Meanwhile, the Wirrn try to board the transport ship but Rogin and Vira fire up the engines to warn them off.
Vira warns the Doctor that the swarm is making its way to the transport deck via the outer hull of the Ark. The Doctor tells Rogin to cut the power, initiate automatic take off and evacuate the ship. He and Sarah rush to the transport deck, helping the others back into the Ark as the swarm enters the ship. The locking mechanisms need to be released so that the transport ship can launch. Rogin and the Doctor each tackle one of the three locks, meeting at the third. The Doctor orders Rogin to leave but Rogin, realising that whoever stays will get killed in the back blast, knocks the Doctor out. Rogin drags the Doctor to safety on the other side of the hatch before returning to the mechanism. Rogin is able to free the last lock just before the transport ship engines fire, killing him instantly.
Returning to the control room, the Doctor wonders aloud whether Noah still had some vestige of human spirit left in him, and was one step ahead of them all along by leading the swarm into the ship knowing what they would do. This turns out to be correct, as Noah "neglects" to engage safety features for the engines. He transmits one final good-bye to Vira before the transport ship explodes with the swarm on board. Mankind is safe to repopulate the Earth.
However, before that can happen, the transmat to Earth must be repaired. The Doctor offers to go down to the planet and repair the receiver unit — it is probably just some corrosion, a quick fix, and it will give him a chance to see if Earth is viable again. Harry and Sarah insist on coming along, and the Doctor tosses a bag of jelly babies to Vira as they use the transmat to beam down to Earth.