In this, the final episode of the trilogy that began with Star Trek II: The Wrath of Khan (1982), the crew of the Enterprise leaves Vulcan after three months of exile, having voted to return to Earth to face charges for their actions in Star Trek III: The Search for Spock (1984). They depart for Earth on the Klingon Bird-of-Prey they captured from Commander Kruge on the Genesis Planet, now rechristened by Dr. McCoy as the "HMS Bounty". After performing modifications to their new craft, the crew take off and head for Earth.
Having had his katra successfully returned to his body, Spock has been in rigorous training sessions to restore his mind. He is able to answer many rapid-fire questions on Vulcan history, science and philosophy but when the testing computer robotically asks him "How do you feel?" Spock doesn't know the answer. His mother, Amanda, informs him that part of the retraining of his mind also involves the human component, which is guided by emotion. She bids him to seek the answer when he leaves with Kirk and the rest of his shipmates. Spock is reinstated as the craft's science officer, a move by Kirk which is questioned by McCoy, who thinks Spock isn't fully up to the challenge.
Meanwhile, an alien probe of unknown origin is also on course for Earth. As it travels, it emits a strange signal that disables all of the technology that it encounters, leaving a trail of damaged and powerless starships and starbases in its wake. Upon its arrival on Earth, the planet itself is jeopardized as high technology fails, the oceans are evaporated, and the atmosphere is ionized. Clouds block out the sun, which Earth cannot survive without. Violent storms break out across the Earth's surface, threatening all of humanity and severely disabling all communications.
Arriving within our solar system, the Bounty crew receives a distress transmission from the Federation President on Earth who informs them that all craft must stay away from Earth. The Bounty crew analyzes the strange signals from the probe and realizes they are those of whale song, an apparent attempt to communicate with the humpback whales that formerly lived in Earth's oceans but were hunted to extinction in the 21st Century. Kirk asks Spock if the whales song could be simulated, however the language could not be duplicated and a response would only sound like gibberish. The crew decides that the only way to answer the probe's signals is to travel back in time and obtain humpback whales. Kirk orders Spock to begin calculations for time travel and has the Enterprise's chief engineer, Mr. Scott, plan to enclose part of the ship's cargo hold to serve as a storage and transport tank for the whales. Kirk is able to send a final communication to Starfleet on Earth informing them of their plan but it is quickly disrupted by the extreme weather patterns.
Using the slingshot-around-the-sun maneuver first developed on the television series, they take the Bounty back to 20th Century Earth where two humpback whales are detected in the vicinity of San Francisco. Cloaked, the Bounty lands in Golden Gate Park, but the strain of time travel has depleted her energy source of dilithium crystals and they must find a way to re-energize them. Spock suggests that since nuclear fission still exists as a power source in the 20th Century, they may be able to capture nuclear particles to rebuild the dilithium crystals.
Once in the city, the crew separates to accomplish three different missions: Locate the whales, build a tank within the Bounty that can carry the humpback whales, and locate a source of high-energy photons with which the crystals can be regenerated. Kirk (William Shatner) and Spock (Leonard Nimoy) will locate the whales, Dr. McCoy (DeForest Kelley), Scotty (James Doohan) and Sulu (George Takei) will find materials to build the tank and Chekov (Walter Koenig) and Uhura (Nichelle Nichols) will find a nuclear energy source to repair the dilithium core. The team finds their way into downtown San Francisco and discover that society of the 20th Century is still using money. Kirk sells the antique eyeglasses given to him by McCoy to an antique dealer for $100 and splits the money between himself and the other teams.
Kirk and Spock go in search of the two whales, locating them at Sausalito's Cetacean Institute (a redress of the Monterey Bay Aquarium). There, they encounter Dr. Gillian Taylor (Catherine Hicks), the assistant director of the Institute and keeper of the whales who are named George and Gracie. During a regular tour Gillian mentions that the whales will soon be returned to the ocean since the institute can't afford to keep them in captivity. Gillian also says she's grown quite attached to them, as they are gentle, friendly and intelligent animals. Spock dives into the whale tank and mind-melds with one of the whales, but is caught in the act and Kirk and Spock are ejected from the Institute. Walking along Marine Drive in San Francisco, Spock explains that he believes he was successfully explaining their intentions to the whales and that they communicated to him their unhappiness at their treatment by humans. The pair are found by Dr. Taylor and given a ride to Golden Gate Park to drop off Spock. While she drives them there, she asks them questions about their interest in the whales. Spock reveals that Gracie is pregnant and she stops her truck suddenly, somewhat upset and astonished that Spock would know such a fact. To smooth over the tense situation, Kirk suggests that they go to lunch.
Kirk goes to an Italian restaurant with Gillian and eventually informs her part of his plan--he does not divulge that the whales are needed to save Earth in the future; he informs her that he is attempting to repopulate the species. Gillian is incredulous but skeptical. Kirk asks her for the radio frequency of the transmitter the whales will be tagged with and she avoids the question. However, she does reveal that the whales are to be released into the ocean at noon the next day. Kirk suddenly realizes that he is on a much tighter timeline than he figured. When Gillian drops off Kirk at Golden Gate Park and he asks her for the radio frequency, she still refuses to turn it over. As she drives away, she just misses him being beamed back onto the Klingon warbird.
Meanwhile, Dr. McCoy and Scotty set off in search of material suitable for creating a transparent tank wall on the warbird. Since transparent aluminum hasn't been invented yet, they will have to make due with plastic. At a large company called Plexicorp, they use the formula for transparent aluminum as a bribe to obtain the material they need from the plant's chief researcher, Dr. Nichols (Alex Henteloff). Sulu acquires a military helicopter to transport it back to the Bounty. Finally, Uhura and Chekov go to the Alameda Naval Station to obtain high-energy photons from the reactor of a nuclear "wessel" (which turns out to be the USS Enterprise aircraft carrier). The photons are obtained and Uhura is able to return to the Bounty when Scotty's transporter energy is reduced to minimal, but Chekov is captured and interrogated by naval investigators. Attempting escape, he falls, is seriously injured, and is taken to Mercy Hospital.
Overnight, at the Cetacean Institute, the whales have been released, owing to Gracie's advanced pregnancy, and that Gillian's boss thinking it would be easier on her. She furiously slaps her boss and rushes back to Golden Gate Park where she sees Sulu's helicopter lowering the plastic wall into thin air, the cloaked Klingon warbird. Shouting for help, she is beamed aboard, confirming Kirk's story of the previous evening. She is rather overwhelmed but informs Kirk about the whales being let loose during the night. Uhura is able to locate Chekov; McCoy gravely informs Kirk that they cannot leave Chekov behind and at the mercy of 20th Century medicine. Spock concurs, saying that rescuing Chekov, while a potential impediment to their mission, is the human thing to do.
Together, she, Kirk and McCoy rescue Chekov from the hospital. McCoy (after curing a elderly woman's kidney dialysis with a single pill) argues briefly with the 20th Century doctor, who plans to drill into Chekov's skull to relieve pressure from hemorrhaging. McCoy insists that the damaged artery in Chekov's skull should be repaired first and scoffs the "medieval" equipment and techniques of 20th Century medicine. Kirk breaks up the argument by locking the doctor and his staff in a small room and McCoy sets about healing Chekov. They exit the OR and are chased by the police guarding Chekov and beam out of the hospital to Golden Gate Park from inside an elevator. They board and the warbird leaves immediately to recapture the whales from the open sea. The whales are swimming in the Gulf of Alaska and about to be harpooned by a whaling vessel when the warbird decloaks, scaring off the hunters. The extra weight of both the whales and the water that were beamed aboard the Bounty create a new problem: the added mass will make their time travel more difficult. Spock is able to alter their trajectory around the sun and they successfully time warp back to the 23rd Century.
However, the ship is immediately rendered powerless by the probe and crash-lands in San Francisco Bay near the Golden Gate Bridge. Freed into the ocean, the whales respond to the probe and it is finally satisfied, halting the transmission of its dangerous signal and allowing the Earth to survive. The probe leaves Earth and the crew of the Bounty celebrate in the water as the whales swim off into the Pacific.
A brief trial is then held for the crew of the HMS Bounty. Though Spock does not stand accused, he stands with his crewmates as a show of solidarity and gratitude. The charges are serious: conspiracy, assaulting Federation officers, theft of Federation property (the Enterprise), sabotage of the USS Excelsior, willful destruction of Federation property (the Enterprise again), and disobeying direct orders. When asked how they will plead, Kirk answers for everyone, saying they plead guilty. However, as a result of having saved the planet yet again as pointed out by the Federation President, the Federation and Starfleet officials are predisposed towards lenience. All charges arising from their actions in rescuing Spock from Genesis are summarily dropped, save disobeying orders from a superior officer, directed solely at Kirk. The charge results in him being busted down in rank from Admiral to Captain (which they are fully aware he preferred anyway) and he is once again assigned the command of a starship. The President also says that Kirk and his crew saved Earth from its own short-sightedness and its people are forever in their debt.
At the trial's conclusion, Kirk and Gillian part ways, as she has been assigned to a science vessel by Starfleet, since she has begun three hundred years of catch-up learning. Spock meets his father who confesses that he was wrong opposing his son's decision to join Starfleet: he has done his father proud and has friends of good character. At the end, Sarek asks if Spock has any message for his mother, Spock just says that he feels fine. At Spacedock, the crew speculates that they may command a freighter while Sulu hopes for the USS Excelsior. However, they soon learn that they are assigned a newly christened starship; the USS Enterprise-A, a duplicate of the earlier Enterprise. Now at home with a new ship, the crew sets out for a shakedown mission.