The Enterprise is intercepted by an alien vessel of advanced design -- powered by an ion drive. A transporter deposits its beautiful pilot aboard the bridge of the Enterprise, where she uses a device on a wristband to incapacitate the crew. She examines several of the crew, stopping at Spock.
When the crew recovers, Kirk notes that Spock is gone from the bridge. But before he can even guess what happened, an anguished McCoy calls him to sickbay, where he has his answer: Spock's brain has been removed from his head, by someone employing extremely sophisticated surgery.
The Enterprise is able to follow the mysterious vessel's ion trail to the Sigma Draconis system, but they lose the trail before discerning the specific planet. With input from the bridge staff, Kirk finally settles on the unlikely Sigma Draconis VI, a glaciated planet.
Beaming down, the landing party encounters savage men, evidence that the planet supports life. Their tricorders reveal a subterranean complex, sprawling in all directions that can only be sustained by ion power. The savage man they briefly capture wants nothing to do with this place; he regards it as dangerous, and the inhabitants as the "givers of pain and delight".
Following the impulses leads the team to a crude trap: items of value to the primitive tribesman, collected inside a small cave. When they enter, a door slams and the cave is revealed as an elevator. Kirk, Scotty, McCoy, and ... Spock's body, animated by a control headband, will soon learn first-hand about the underground complex.
A woman meets them, and as she is reaching for her bracelet, they stun her. On awakening, she can reveal nothing; her mind operates at a child-like level. But all is not lost; Kirk is able to contact Spock, or rather, his still living brain, via communicator. While conversing with the disembodied first officer, the landing party is surprised and knocked out.
Awakening, they discover their gear has been confiscated and they have been fitted with pain-inducing bands. Several of the primitive tribesmen, likewise fitted with the pain bands, stand guard. The complex is controlled exclusively by women. The control bands are the pain, and the hospitable conditions and women themselves are the delight.
Left alone, the landing party is able to overwhelm their guards, who do not control the pain bands. Escaping, they again contact Spock, who generates a signal to guide them to his location. Soon enough, they find it: a room wherein Spock's brain is housed in a complex control system. There, they meet and eventually overpower the leader, who tells them that Spock is "The Controller", a replacement for the last controller, who has ceased to operate. They learn the Controller is at least responsible for managing the automation of the complex.
The leader, now just as dumb as everyone else, does not even remember how she performed the surgery that moved Spock's brain from his body to its present location. When technical skill is needed, she dons "The Teacher", an elaborate helmet that can transfer the stored knowledge of the builders into her brain -- for awhile. But she may only use the teacher under carefully controlled circumstances.
Unable to obtain her cooperation in what she regards as the betrayal of her people, Kirk reluctantly allows McCoy to don the Teacher. But the machine isn't tuned for his alien brain configurations; the transfer works, but is quite painful -- and a second attempt would kill him.
Nevertheless, he starts to work restoring Spock's brain, reconnecting most of it -- until he starts to forget. Fortunately, he has connected enough that Spock is able to guide him through the rest of the procedure, and Spock's brain is restored.