The Enterprise is on an outward course beyond the fringe of the galaxy heading toward Questar M-17 and a source of mysterious radio emissions. The ship begins accelerating toward M-17 due to hyper-gravity and they detect an imploded "dead" star ahead of them. They're unable to reverse course and as they approach, Uhura picks up another signal. Kirk orders flank speed and they manage to slip into orbit around the star.
Spock starts to calculate a slingshot effect to leave orbit while Uhura detects the radio signal ahead. They spot the source: a gigantic spaceship dozens of times larger than the Enterprise. Spock determines the alien ship is entirely dead except for a slight magnetic flux reading. He determines that it's been in orbit for 300 million years. Kirk orders a party of himself, Spock, McCoy, and Scotty to beam aboard using life-support belts.
Once aboard the alien ship, Spock observes the ship has a hive-like construction and Scotty determines the metal was spun like a spider web. Each pod of the ship has been burst open from the inside. Spock concludes that the ship's crew destroyed their vessel. Kirk contacts the Enterprise and has them maintain a transporter lock, and then they go inside a pod that appears to be the command center. Spock detects an increasing energy source and concludes that sensors are absorbing any ambient energy to maintain minimal function. They continue inside and come to a sealed chamber. Spock phasers the controls open and they enter. Once they do, the door seals behind them and life support activates. They're unable to contact the Enterprise due to an interference field.
Scotty tries to cut through the door only to discover something has drained all of their equipment of energy. They proceed to the command console and find a jury-rigged piece of equipment to shield the room. Something tries to burn its way through the sealed door. Spock concludes the door will hold as long as the energy levels remain constant. The viewscreen activates and reveals an alien insectoid creature giving a repetitive message. Spock manages to translate the message and the alien captain warns that they were drawn to the dead star and a malevolent life form entered their ship. They destroyed their ship rather than risk taking it to another planet, and left the message to warn that their shield only protects the room.
The screen explodes, sending them flying, and equipment throughout the room begins to explode. Kirk calls for beam out and the Enterprise transports them back. However, they appear in the transporter chamber with a fifth passenger: the alien life form, a green energy cloud. Kirk tries to order it transported back out but the energy field encircles all of them and then dissipates into the ship.
Kirk returns to the bridge where everyone reports everything is functioning normally. They activate the bridge defense system and Kirk orders Scotty to arm the self-destruct device case. Spock detects a slightly higher magnetic flux. Life support on two decks shuts down and the crew there switches to life support belt. An emergency alert goes off in Engineering and Kirk, Spock, and McCoy go down there to find that Scotty is trapped in a core hatch and only his belt's force field is keeping him from being cut in half. The systems are frozen and the crew goes to work with cutter beams. They get Scotty free but Sulu reports that something has activated the phaser banks. They're forced to look on as the Enterprise destroys the alien ship. They conclude the life form is overriding their systems and Scotty reports that they can't get to the core to activate self-destruct.
Life support shuts down and something goes through all of the ship's computer banks. Kirk has Spock rig a low-frequency shield similar to the one on the alien ship and place it on the navigation console. Kirk concludes that the star's magnetic field is holding the alien life form within its confines and it needs a ship to escape. The life form addresses them through the bridge systems and informs them that now has the starship it needs. It absorbs all the memory banks and tells them to remove the static shield.
Kirk tries to negotiate and when he refuses to obey, it uses the bridge defense system to fire phasers at him. When Spock tries to intervene, it switches a beam to him. Kirk agrees to release the static control in return for Spock's life. The life form releases Spock and Kirk approaches the static shield... and slaps his life support belt on it, fusing the warp drive controls. The life form tells them to fix the controls and Kirk orders Scotty to go through the override controls. Spock identifies the creature's nature as a form of primal energy that utilizes a starship's control systems: it has become the Enterprise. He determines that the life form is growing stronger.
Kirk has Spock mentally compute the slingshot effect to avoid the life form detecting their calculations. It agrees to let Spock aid Scotty, then tells them to plot a course to the heart of the galaxy. Spock notes the life form can reproduce by mitosis and take over every starship and planetary computer system they encounter. The auxiliary controls are ready to activate. Kirk tells the life form the auxiliary controls can only be activated manually.
The life form orders them to proceed and Kirk activates the controls, plummeting the Enterprise out of orbit into the dead sun. The life form opens fire on Kirk, protected by the force field fused to the navigational controls. The life form, panicking, tells them to stop and Kirk tells Spock to activate the warp drive controls. The alien life form abandons the Enterprise as they go into warp and pass through the star and out the other side. The life form is now trapped in the sun and as they leave, it begs for them not to abandon it. The Enterprise proceeds on its starcharting mission.