While in deep space, the Doctor, Amy and Rory receive a communication cube containing a distress call from a Time Lord. Tracing the source of the call to a rift leading outside the universe, the Doctor deletes part of his TARDIS to generate enough energy to traverse the rift. After landing in a junkyard on a solitary asteroid, the TARDIS shuts down and its matrix disappears. The three explore, and meet the strange inhabitants, Uncle, Auntie, a green eyed Ood called Nephew and an excited young woman named Idris who fawns all over, and then bites, the Doctor. While Nephew locks up Idris, Amy and Rory return to the TARDIS, the Doctor follows the distress signal and finds a cabinet containing a large number of message cubes. Upon further investigation of Uncle and Auntie, he finds they are constructed of body parts from other beings, including Time Lords. They are controlled by the asteroid, called House, which is sentient and able to possess other technology around it. House led the Doctor there and removed the TARDIS' matrix, initially intending to consume its Artron energy. Upon learning that the Doctor is the last Time Lord and that no more TARDISes will ever arrive, House transfers itself into the TARDIS to escape from the rift. Amy and Rory are trapped inside as the House-controlled TARDIS dematerialises.
The Doctor learns that Idris contains the personality of the TARDIS' matrix. Idris, as the TARDIS, and the Doctor learn they selected each other hundreds of years prior when the Doctor fled Gallifrey. Without House's support, Uncle and Auntie die, and Idris' body only has a short time before it also will fail. Idris reveals that House had stranded many TARDISes before, and that its universe is hours away from collapsing. The Doctor and Idris work together to construct a makeshift TARDIS from scraps, and then pursue House.
Aboard the Doctor's TARDIS, House threatens to kill Amy and Rory. He plays with their senses as they try to flee through the corridors, then sends Nephew after them. Idris makes a psychic connection with Rory to give him directions to a secondary control room, where he and Amy are able to lower the TARDIS shields without House's interference. This allows the Doctor to land the makeshift console in the secondary control room, which atomises Nephew. House deletes the secondary control room as he prepares to break through the rift, which the Doctor anticipates. The TARDIS safety protocols transfer them to the main control room, where the dying Idris releases the TARDIS matrix back to the TARDIS, destroying House. A remnant of the TARDIS matrix, in Idris' body, sadly states that she will not be able to speak to the Doctor again but will be there for him. Idris' body then disappears as the TARDIS matrix is fully restored.
The Doctor installs a security field around the matrix to prevent it from being compromised again. Rory asks the Doctor about some of Idris' final words—"the only water in the forest is the river"—but the Doctor doesn't understand. After Amy and Rory leave to find a new bedroom, the original having been purged by House, the Doctor talks to the TARDIS, and, in response, a nearby lever moves on its own, sending the TARDIS to its next destination.