The TARDIS materializes in a garden centre in 1963, and while the Doctor buys a begonia from surly clerk Fitz Kreiner, Sam is attacked by Austen, a madman who is fleeing from the nearby manor house. Fitz accidentally helps to subdue Austen, who is babbling about a cave where devils feed off the flesh of the living. The Doctor helps Austen’s minders, Dr Charles Roley and Nurse Maria Bulwell, to return him to the house, where he learns that six former psychiatric patients are participating in Roley’s private study. Although the patients have never met before, they have all seen visions of the same cave of devils. Roley believes that they have tapped into a common human archetype, and that by forcing them to confront their visions and go deeper within them he may learn more about the collective unconscious and perhaps how to tap the beneficial side-effects of madness. The Doctor, however, fears that Roley is forcing his patients to confront traumas they aren’t ready to deal with -- especially since the Doctor has seen the cave himself, on a distant alien planet.
Sam decides to spend the night out on the town, and Fitz agrees to show her the sights. Fitz’s mother, one of Roley’s patients, has been in and out of hospitals for most of her life, and his father was a German who fled his home to escape the Nazis only to face persecution in England for his nationality. Fitz has suffered the same sort of persecution and has become cynical and surly in response. He takes Sam to a nightclub, but a drunk patron makes a clumsy pass at her and she storms out when Fitz does the same. Outside, however, the drunk man is killed by the half-robot Azoth and his companion Tarr, who have been waiting for Sam to emerge. Azoth attempts to read Sam’s mind, and she is concussed while trying to fight them off. Fitz uses fake voices to convince them that an angry crowd is approaching, and they flee. Fitz, not wanting to get involved in the murder investigation, takes Sam back to his bedsit to recover.
The Doctor spends the night reading Roley’s files, and learns that his patients have all developed strange cysts from which Roley has removed organic growths. Unable to fit the growths into his theories, Roley has dismissed them as irrelevant, but the Doctor discovers that Austen’s has grown back. Bulwell unexpectedly agrees that the Doctor should be allowed to study the growths, but this is only a ruse to get him out of the way. She is deeply in unrequited love with Roley and will do anything to see him succeed, and while the Doctor is studying the growth -- which resembles a giant black leech -- Roley gets back to his research without the Doctor’s interference. Roley uses sensory overstimulation and hypnotism to regress war veteran Dafydd Watson back through his past memories, but is surprised when Watson unexpectedly regresses to the life of his great-grandfather -- also a mental patient who believed his identical twin had been kidnapped by the Devil. At the same moment, the leech which the Doctor is studying comes to life and tries to burrow into his hand, but another of Roley’s patients, Peter Taylor, breaks into the lab and takes it. Taylor, Watson, and the other patients -- Lucy, Russell and Muriel Kreiner -- are all chanting the same cabalist creed; to do what thou wilt shall be the whole of the law. Before the recovering Doctor can stop them, they take the leech to Austen’s room and cut open his spine to replace it, killing him.
Sam awakens in Fitz’s room, and soon storms out, infuriated by his cynical sexual innuendoes and insistence that he’s not involved in the clubgoer’s murder. Shortly afterwards, however, the police arrive to question him about the murder, and he impulsively flees to Roley’s house. Tarr spots him boarding the bus, and he and Azoth set off in pursuit. The Doctor, meanwhile, connects Austen’s leech to a computer model he’s made of Roley’s brain, and watches as the leech adds new synaptic structures which alter the function of the model brain. At the same time, however, the leech is transmitting a signal which leads Azoth straight to the house. Fitz has just arrived when Azoth and Tarr break in, kidnap Sam and Taylor, and take Austen’s body for analysis. The Doctor remains unaware that anything is going on outside the laboratory until it’s too late.
Fitz recognized Tarr from an old family photograph, as one of his great-grandfathers from his mother’s side. The Doctor reveals that the “leeches” are the product of biological engineering, designed to alter the human brain to run a programme of some sort. The cave in the visions must be a real place to which the patients’ ancestors were taken 150 years ago and implanted with the programme. But something has gone wrong and the code has become corrupted through the generations, driving the afflicted mad with visions they interpret as images of Hell and damnation. The Doctor sets off to find Azoth, rescue Sam and find out what the programme was originally intended to do, and advises Roley to keep an eye on the patients. Watson, however, has woken from his relaxation therapy feeling stronger than ever, and his new confidence is being passed on to the other patients -- as well as other things. Working together, he and Lucy are able to exert some telepathic control over Roley and Bulwell; they only succeed for a moment, but if all five of the patients were to work together who knows what could happen?
Sam awakens in the cave from the visions, where Azoth intends to run the programme in her brain and see what happens; he reveals that he has chosen her because of her unusual genetic structure, which causes her to briefly remember her vision of a dark-haired Sam who lives in a bedsit in King’s Cross. Before she can do anything Azoth implants a leech in her body, and after a moment of pain she suddenly sees horrific creatures feeding both on Tarr and herself. Azoth sends Tarr and Sam out to the street where she sees the Beasts feeding upon every living thing in sight, but she manages to overcome her revulsion long enough to escape. Azoth’s memories are now returning; he had kidnapped identical twins during his initial experiments, implanting the programme in one twin while the other acted as a control. When he tried to place the Taylor control in a cryogenic unit, however, the man struggled with him and accidentally caused the unit to explode, killing Taylor and seriously damaging Azoth. Azoth shut down for repairs for 150 years, at which point he awoke with no memory, and woke Tarr from cryogenic suspension in the hope that Tarr could tell him what he’d been doing. Now that he has remembered, Tarr no longer serves any purpose, and Azoth kills him and prepares to reprogramme Peter Taylor and carry out his mission to save the world.
The Doctor finds Sam, who tells him her story and informs him that Azoth referred to the “Benelisa programme”. The Doctor recognizes the name; the Benelisans were an advanced species who were wiped out centuries ago by a mysterious catastrophe. The Doctor now realizes that the Beasts, which exist in another dimension adjacent to this one, must have descended upon the Benelisan homeworld and consumed the entire species; before dying, however, the Benelisans sent out robots such as Azoth to track down and wipe out the Beasts. But they must first modify the brains of the Beasts’ potential victims to run their programme; and on Earth, the programme has gone wrong. The Doctor returns to Roley’s mansion, only to find that Watson and the others have used their new powers to kill the maid, and to destroy Roley’s sanity by altering his mind to match the new pattern on the Doctor’s computer model. When Sam arrives, her own leech -- which carries a less corrupt version of the programme -- interferes with their leeches’ cross-talk, temporarily robbing the madmen of their new powers. The Doctor administers anaesthetic to them, but realizes too late that this is also affecting Sam -- her version of the programme is also imperfect, and she collapses, dying, as the changes to her own mind continue.
The Doctor, realizing that Fitz also has a leech in his body thanks to his mother’s genetic legacy, removes it and uses it to link with the Beasts through Sam’s mind. He learns that they are not malevolent -- they are simply animal parasites who feed on other worlds and then go. Usually they pass through without harming the native population; the Benelisans were wiped out because their numbers were so low. Azoth’s crusade is unnecessary; the Benelisans’ extinction was an unfortunate accident. The Doctor has Fitz help him to carry Sam back to the TARDIS and takes her to Azoth’s cave, where he tries to convince Azoth to help cure her. Azoth, knowing from his scan of Sam’s mind that the TARDIS is a time machine, refuses to do so unless the Doctor takes him back in time to destroy the Beasts and prevent the extinction of the Benelisans. When the Doctor refuses, Azoth attacks him -- but Azoth is still disoriented, and accidentally destroys part of his laboratory, freeing Taylor. Taylor flees, following the call in his mind back to Roley’s home. Azoth, believing his programme has failed, prepares to implement the Final Solution and destroy all life on Earth to eradicate the Beasts, but Fitz bluffs Azoth into believing that he can see and destroy the Beasts himself. Azoth shuts down, satisfied that his mission has succeeded.
The Doctor sends Fitz to stop Taylor from reaching Roley’s mansion; the patients’ proximity and Roley’s misguided attempts at therapy have caused the malfunctioning leeches to cross-circuit their minds and boost their dormant psychic powers. The more of them there are, the more their powers will grow. Fitz fails to stop Taylor, however, and once he rejoins the others they find they all have the power to kill with a touch. They kill Bulwell, and prepare to spread their message of pain to the world. The Doctor, meanwhile, links Azoth to the TARDIS telepathic circuits and uses them to find a cure for Sam. He then takes the TARDIS to Roley’s mansion to cure the patients, but he’s accidentally reactivated Azoth, who lurches out of the TARDIS to carry out his Final Solution. Watson destroys Azoth instead, and the Doctor is thus unable to cure him and the others. By this point they don’t want a cure -- they want revenge for the treatment they’ve suffered at the hands of those who would “cure” them.
The Doctor escapes with Azoth’s head and shelters with Fitz in the wine cellar, where he changes Azoth’s “Final Solution” -- instead of killing every living thing on Earth it will affect only those infected with Azoth’s leeches. In his haste, however, he’s left the TARDIS door open, and Watson captures Sam and threatens to kill her unless the Doctor surrenders. The Doctor has no choice but to activate Azoth’s Solution, and the bioelectric pulse focussed through the leeches causes the patients’ heads to explode. Fortunately, Sam survives as her leech had been destroyed by the Doctor’s cure. The Doctor phones in an anonymous tip to the police, and since Fitz’s mother is dead and he is wanted by the police, the Doctor invites him to join them. Fitz accepts. As they depart, the police arrive to find a mansion full of bodies and a babbling, insane Charles Roley, who claims that devils are covering them all, feeding off them...