In Mostly Harmless, the fifth and final book in the Hitchhiker's Guide to the Galaxy trilogy, Arthur Dent has taken up work as a sandwich maker and resigned himself to a quiet life on the planet Lamuella, content in the knowledge that he can't die just yet. His life is once again thrown into chaos and confusion when his old friend Trillian appears with a young girl named Random, whom she claims is Arthur's daughter. With plenty of puns, the novel brings about the end of the earth.
Arthur Dent plans to sightsee across the Galaxy with his girlfriend Fenchurch, but she disappears during a hyperspace jump, a result of being from an unstable sector of the Galaxy. Depressed, Arthur continues to travel the galaxy using his biological donations to DNA banks to fund his travels, and knowing that he cannot die until he visits Stavromula Beta, as told to him by the insane Agrajag who had been repeatedly killed in various ways by Arthur before being reincarnated. During one trip, he ends up stranded on the homely planet Lamuella, and decides to stay to become a sandwich maker for the local population.
Meanwhile, Ford Prefect has returned to the offices of the Hitchhiker's Guide, and is annoyed to find out the original publishing company, Megadodo Publications, has been taken over by InfiniDim Enterprises, which are run by the Vogons. Fearing for his life, he escapes the building, along the way stealing the yet-unpublished, seemingly sentient Hitchhiker's Guide Mk. II. He goes into hiding after sending the Guide to himself, care of Arthur, for safekeeping.
On Lamuella, Arthur is surprised by the appearance of Trillian with a teenage daughter, Random Dent. Trillian explains that she wanted a child, and could use the only human DNA she could find, thus claiming that Arthur is Random's father. She leaves Random with Arthur to allow her to better pursue her career as an intergalactic reporter. Random is frustrated with Arthur and life on Lamuella; when Ford's package to Arthur arrives, she takes it and discovers the Guide. The Guide helps her to escape the planet on Ford's ship after Ford arrives on the planet looking for Arthur. Discovering Random, the Guide, and Ford's ship missing, the two manage to find a way to leave Lamuella and head for Earth, where they suspect Random is also heading to find Trillian. Ford expresses concern on the Guide's manipulation of events.
Reporter Tricia McMillan is an alternate version of Trillian living on Earth who never took Zaphod Beeblebrox's offer to travel in space. She is approached by an extraterrestrial species, the Grebulons, who have created a base of operations on the planet Rupert, a recently discovered tenth planet in the Solar System. However, due to damage to their ship in arriving, they have lost most of their computer core and their memories, with the only salvageable instructions being to observe something interesting with Earth. They ask Tricia's help to adapt astrology charts for Rupert in exchange for allowing her to interview them. Tricia conducts the interview, but the resulting footage looks fake. She is called away from editing the footage to report on a spaceship landing in the middle of London.
As Tricia arrives, Random is leaving the ship. Random yells at Tricia, believing Tricia to be her mother. Arthur, Ford, and Trillian arrive and help Tricia to calm Random. They remove her from the chaos surrounding the spacecraft and take her to a bar. Trillian tries to warn the group that the Grebulons, having become bored of their mission, are about to destroy the Earth. Random disrupts the discussion by producing a laser gun she took from her ship. Arthur, still believing he cannot die, tries to calm Random's nerves, but a distraction causes her to flinch. She fires the weapon, sending the bar into a panic. Arthur tends to a man hit by the blast, and learns he is another reincarnation of Agrajag. He also discovers that the bar is named "Stavro Mueller– Beta". Arthur makes out Ford laughing wildly at this turn of events. Arthur, despite the panic beginning around him, experiences a "tremendous feeling of peace".
The Grebulons, having determined that removing Earth from the astrological charts will improve their horoscopes, destroy it. It is revealed that the Vogons designed the Guide Mk. II with the ability to see the potential outcome of any event, enabling it to ensure that every version of the Earth in all realities is destroyed. With its mission complete, the Guide collapses into nothingness.