In a home in modern-day London, child psychologist Dr. Moon helps a young girl see herself in a library. She talks about how the library goes on forever and somehow something has gotten into her library. Although Dr. Moon tries to assure her the library is in her mind, she sees the Doctor and Donna barge in ahead of a pursuer, and then the Doctor notices her and apologizes.
The Doctor and Donna arrive in the TARDIS. The Doctor explains that the Library is so big that it doesn't need a name. It's an entire world with an index computer for a core. Every book ever written is stored there. They are in the biography section, but the Doctor warns Donna not to read the future books to avoid spoilers. It takes a few minutes for him to notice that no one is there. He accesses a computer system and determines they're the only two humanoid life forms on the planet. However, when he expands the scan to any other life forms, he detects a million million other creatures. Donna wonders if the books are alive, but they're interrupted when they hear a voice. Going back inside, they find a Node, a mechanical information device with a human face. It warns them that it has a message from the Head Librarian: the library is sealed off and they should run. The Doctor uncovers a second message: "Count the shadows."
The Doctor and Donna walk through the stacks and the Doctor admits he got a message on his psychic paper, saying to come to the library. The Doctor doesn't know who the message is from. They hear a clicking noise and see the lights start to go off. They try to get through a jammed door and finally knock it down. The Doctor bars the door with a book, and they encounter… a small spherical security camera. The Doctor apologizes to it and it shuts off.
On Earth, the girl says they've arrived and she hears an annoying noise: the Doctor using his sonic screwdriver on the security camera. He activates it and the girl collapses. She yells for them to stop and the words flash on the security camera's screen. The girl informs Dr. Moon and her father that the Library is breached and others are coming. Donna and the Doctor see the message and Donna tries to get information from a Node. It reveals that its face is taken from a human that made a donation. Donna is not thrilled, but the Doctor notices a shadow with nothing to cast it. More lights are going out, but the shadow they just saw has disappeared. The Doctor realizes that the shadow has moved. The Node informs them that the Library has been breached and others are coming. Suddenly the door bursts open and suited figures enter. A woman opens her visor and greets the Doctor. She introduces herself as Professor River Song, in charge of an expedition paid for by Strackman Lux (also present).
The Doctor tries to convince them to leave without success and Strackman Lux has his assistant Evangelista try to get them to sign a contract relieving Lux of responsibility. Song says the Library has been abandoned for a hundred years and they're there to find out what happened. The Doctor notes that all species have a fear of the dark, which is actually the fear of the Vashta Nerada. He tells the party to get as much light as possible.
As they make preparations, Song reveals that she's the one who summoned the Doctor, and wonders why he's pretending he doesn't know her. She soon realizes that she knows the Doctor from his future, checking a diary to see where he's been. She notes the Doctor is younger than she's ever seen him, and he says he doesn't know who she is.
Song's team try to trap into the security protocols and set off a sound that Donna recognizes as a phone. On Earth, the girl's phone rings. However, only she can hear it. The Doctor accesses the system and appears on the girl's TV. They talk briefly but are cut off. As the Doctor works to restore the lights, he notices Song's diary and considers reading it, but Song says it's against the rules: his rules.
The girl uses the remote to try to get the Doctor back and finds a secret panel on the controller. She presses a button and in the Library, books start flying off the shelves. The Doctor notes that a CAL program is blocking his access and tries to get answers from Lux. Lux refuses until they sign the contract, but finally admits it has taken three generations for them to gain access to the Library. Song reveals there was one additional message: "4,022 saved, no survivors." There were 4,022 people in the library when it was sealed off. As they argue, Evangelista wanders off through an open panel and into a hall of books. The Doctor and the others hear her scream and run to the hall… where they find Evangelista stripped to the bone in only a few seconds.
As they stare at the skeleton, Evangelista speaks to them through her communicator. Song explains that she is a data ghost, her consciousness temporarily copied into the telepathic communicators. The pattern quickly breaks up and fades away and Song shuts down the communicator for good. The Doctor goes to find a packed lunch while asking Song who she is to him. She refuses to answer, much to his disappointment.
On Earth, Dr. Moon talks to the girl in private and tells her that the real world is a lie and her dreams are real. He warns that "the shadows are moving again" and only she can save the people trapped there.
While the Doctor works on the floor with his sonic screwdriver, Song notes that Donna is the Doctor's current companion and that he hasn't met Song yet. She sent the message to the Doctor but it arrived too early. She recognizes Donna's name but hasn't met her, and Donna wonders why they haven't met in the future. The Doctor then explains that the shadows aren't shadows: they're a flesh-eating swarm, the Vashta Nerada. He demonstrates using the packed lunch, and notes that most planets have them in small clusters but they're not typically aggressive. The Doctor says all they can do is run, and Donna suggest they find a teleport point at a local shop.
As they prepare to leave, the Doctor notices that one of Song's team, Dave, now has two shadows and that the Vashta Nerada have latched onto him. The Doctor attaches the helmet and tries to seal it using his sonic screwdriver. Song reveals that she has one as well, and seals the other suits. He take Donna to the teleport point and sends her to the TARDIS: when she protests, he sends her away before she can arrive. However, as she arrives her pattern breaks up as she screams in agony.
The Doctor returns to the others only to discover that Dave's second shadow has disappeared. Dave says that the lights have gone off and when they look into his helmet they see that it's filled with the Vashta Nerada. He talks to them but they quickly realize it's a mental data ghost on his communicator: he's dead. The suit, animated by the Vashta Nerada, grabs the Doctor but Song breaks him free. The animated suit walks toward them as the shadows spread out from it. Song blasts her way through the wall with a squareness gun and they run into the shelves.
On Earth, the girl tells her father that Donna Noble has been saved.
Taking refuge in the shelves, the Doctor tries to boost the lights to slow down the Vashta Nerada. Song explains that the Doctor gave her a sonic screwdriver, although he notes that he doesn't give one to just anyone. The Doctor realizes that Donna hasn't arrived at the TARDIS. He goes to a Node, which has Donna's face. It says that Donna has left the Library and has been saved.
The animated suit closes in and they're forced to keep running. However, the Vashta Nerada appear in front of them as the suit closes in, trapping them.