In 2286, Kirk and company wait in self-imposed exile on Vulcan, monitoring Spock's progress as he remembers his past. On Earth, the Federation Council is urged by the Klingon Ambassador to produce Kirk for trial for his actions against the Klingons on the Genesis Planet. Ambassador Sarek denies the request to permit Kirk's extradition from Vulcan and defends Kirk before the Council. The council agrees, yet demands Kirk return to Earth to face his violation of nine Starfleet regulations. Caught between two different sides, Kirk finally decides to take the Klingon Bird-of-Prey to Earth to face the Council's charges. Spock, still suffering slightly from his fal-tor-pan, decides he must accompany his captain.
The crew arrives to find Earth in turmoil, threatened by a deep space probe that has drained all power and is vaporizing the oceans, producing a worldwide cloud cover which begins to block the sun. Because of the enormous storms ravaging the entire planet, Starfleet warns all approaching spacecraft to keep their distance.
Spock manages to identify the probe's unusual communication, which turns out to be a transmission to long-extinct humpback whales. When the probe's signal is not answered, it increases its output, causing more clouds and even more storms. Kirk and crew, orbiting the doomed planet, decide that the only way to save Earth is to time-travel back to the twentieth century, when whales still existed, and return to the present with a pair of the mammals. With any luck, then the probe's signal might be answered and Earth spared.
The crew, still piloting the Bird-of-Prey, use the slingshot effect to travel back in time. They pick up whale songs emanating from the West Coast of North America. They land the Klingon warbird in a San Francisco park, where thanks to its cloaking device, the ship is concealed from view. However, the time traveling damaged the ship's valuable dilithium crystals. The small crew then splits up, with Uhura and Chekov assigned to locate an atomic reactor for photons to recharge the ship's power, and McCoy, Scotty and Sulu ordered to create a tank in the cargo bay that will safely house a pair of whales.
Kirk and Spock, after a humorous trip through twentieth century San Francisco, locate Dr. Gillian Taylor and her mated pair of whales, George and Gracie. Due to cut-backs at the Maritime Cetacean Institute, where the whales are on exhibit, they are due to be released in the sea. Once there, Taylor fears the pair will be in danger from whale hunters. When Spock performs a Vulcan mind-meld with the whales, he learns that Gracie is pregnant. Kirk tries to convince the doctor that he's from the future and that Starfleet will take good care of her whales, but Taylor is, understandably, incredulous.
Meanwhile, Chekov and Uhura have located enough photons from a reactor to complete their mission — from the U.S.S. Enterprise CVN 65 aircraft carrier, no less. Scotty, however, can't beam both crew members back at once. Faced with a decision, Chekov sends Uhura up with the needed photon collector. Chekov is then captured and questioned by FBI agents. The Russian escapes, is chased by Marines, and eventually falls and ends up hospitalized, in critical condition.
McCoy and Scotty have concurrently arranged a trade for a large piece of Plexiglas to build a whale tank on board the Klingon ship.
Kirk finally is able to convince Gillian Taylor to help him, first in retrieving Chekov, then the whales. Joined by McCoy, the three pull off a wild rescue, springing Chekov from the hospital. With twenty-third century medical technology, McCoy is able to quickly repair Chekov's otherwise fatal condition. Kirk says goodbye to Gillian, assuring her that her whales will be safe and, what's more, will be saving the entire planet in the future. At the last second of Kirk's transport, she throws her arms around him, transporting herself to ship along with the captain.
Using the radio frequency provided by Gillian, the crew locate the whales, now at sea. Just as they are about to transport George and Gracie on board the Klingon ship, a whaler ship comes into view, threatening the two mammals. The future of Earth at stake, the Bird-of-Prey valiantly fights off the whaler and beams up the whales, along with tons of water, to the prepared tank in their cargo bay.
Off the planet and en route to the Sun for the time-traveling slingshot maneuver, Spock makes his calculations and the ship is propelled to the twenty-third century. Arriving at Earth, the ship is rendered powerless by the probe and crash-lands in San Francisco Bay. Kirk then releases the whales and they answer the probe. Apparently satisfied, the probe retreats back into deep space once more.
With Earth safe and the storms gone, Kirk and his crew are brought before the Federation Council to be reprimanded for stealing and destroying the U.S.S. Enterprise in the rescue of Spock. Although Kirk disobeyed direct orders and violated numerous Federation laws, the Council praises the Admiral and his crew for saving Earth. And even though Kirk is "demoted" to Captain, he is given the only job he ever wanted — the command of a starship. And the ship he is given? A new U.S.S. Enterprise, registry NCC-1701-A.