The Doomsday Machine

On stardate 4202.9, the USS Enterprise responds to a distress call and finds that several planets in a nearby system have been destroyed. They soon find their sister ship, the USS Constellation, adrift and heavily damaged. Captain Kirk and a damage control team beam over to investigate and find the ship's commanding officer, Commodore Matthew Decker, holed up in the auxiliary control room – apparently the sole survivor. Mr. Scott reports that the ship's warp engines are damaged beyond repair and the weapons exhausted. Meanwhile, an incoherent Decker can only mutter about something attacking his ship.
The logs reveal that the ship investigated the break up of a planet and was soon attacked by an enormous machine with a conical shell miles in length and a giant opening at one end filled with sparkling energy. After the attack, Decker ordered his surviving crew to the surface of a nearby planet, but to his horror, the machine destroyed that world next. Spock theorizes the machine breaks down planets into rubble which it then consumes for fuel and adds that given its past trajectory, it is likely to have come from outside the galaxy.
Kirk theorizes that they've encountered a doomsday machine, a device built to destroy both sides in a war. It was intended as a bluff or deterrent, not to be actually used, but was activated nonetheless. It wiped out its builders long ago but it lives on indefinitely, fueled by the very planets it destroys.
Kirk has Decker beamed to the Enterprise while he and Scott remain on the derelict. On the Enterprise bridge, Mr. Spock is alerted to the approach of the alien machine which generates interference that makes radio contact with Starfleet Command impossible. As the machine attacks, Decker comes to the bridge, and quoting Starfleet regulations he pulls rank on Spock and assumes command. He then orders a full on attack against the machine ignoring Spock's warning that the ship's weaponry is ineffective against the doomsday machine's pure neutronium hull. As a result, the warp engines are disabled and the Enterprise becomes drawn by a tractor beam towards the machine's glowing maw.
Aboard the Constellation, Scott has managed to restore partial phaser and thrust control, and Kirk creates a diversion to distract the planet-killer away from the Enterprise. As the machine veers off, Kirk orders Spock to relieve Decker of command as he is in no condition to give orders. After protest, Decker finally yields, but he knocks out his guard and quickly heads to the hanger bay and steals a shuttlecraft. He then pilots it on a kamikaze course into the planet killer's maw despite the pleas of Kirk and Spock to turn back.
Kirk learns that the shuttlecraft explosion slightly decreased the planet-killer's output power. He realizes Decker may have had the right idea but not enough energy to succeed. Kirk has Spock determine if the thermonuclear detonation of the Constellation impulse engines inside the planet-killer would destroy it. Spock confirms that it would, but he and Scotty object to Kirk's intention to remain on the Constellation to carry it out. Kirk has Scotty rig a manual 30-second detonation timer, planning to start it and beam back to the Enterprise before detonation. Scotty explains that once the timer is started, it cannot be stopped.
With everything prepared, Kirk orders the others back to the Enterprise and steers the Constellation toward the planet killer's maw. At the right moment he starts the 30-second timer and asks to be beamed out. In classic cliffhanger fashion, the damaged Enterprise transporter shorts out. Kirk is stranded on the Constellation. Scott rushes to make repairs. As the the timer ticks toward zero, Kirk issues an understated request: "Gentlemen, I suggest you beam me aboard" and watches the doomsday machine grow on the viewscreen. Scott's desperate fix succeeds and Kirk is beamed off the Constellation at the very last second. The Constellation enters the maw and explodes, destroying the planet-killer's mechanism and leaving its indestructible shell adrift, dead in space.
In the epilogue, Kirk and Spock muse about the parallels between their doomsday machine and the "doomsday machines" of late 20th century Earth, nuclear weapons. Kirk notes with irony that the Constellation's impulse engines exploded in the same way, though this time it served a constructive purpose.