The episode doesn’t offer any explanation for it, but somehow Spock is able to break through the childrens’ garbling of Jim’s speech, by touching his qui’lari, the focal points of a mind-meld, and concentrating. Spock himself looks surprised by this.
Immediately after, when Jim himself is attacked and his fear takes him over, he lets out the softest whine. The camera shifts to Spock, and free of the influence of the aliens, his expression softens and he goes to his Captain’s side.
Spock gets through of the aliens’ influence over him by concentrating on Jim – given the cues of the qui’lari points? He’s using their bond. Spock draws on his bond with Jim to listen to him clearly, to break free. And then so does Jim.
He latches on to Spock, he’s trying to ground himself. Jim Kirk is having a full-blown melt-down, he’s being faced with his greatest fear of all. We’ve seen this in him time and time again. As it escalates, he breaks away from Spock.
Spock tries to get to him.
Captain Kirk latches on to him again. He holds close to the only thing that’s still real. Captain? Not if he’s lost the Enterprise. Captain Kirk? But Spock, Spock seems to think so. That’s not what breaks through.
Jim.
That’s what reaches him. Spock calling out his name. Jim holds his breath. He stares at Spock, registering the single word he’s said. They hold eye contact, Jim’s gaze breaks away, and he begins to breathe again. Spock’s spoken only his name and breathed life into him, brought Jim back to himself. When Jim looks at Spock again, he’s nearly fully composed.
Spock used his bond with Jim to free himself, then returned the favor. They draw strength from each other. They rely on each other, at their weakest, to regain control. They’re the only ones on the Enterprise able to overcome the fear by themselves, and they do so because their bond is so strong.