At the time of the story, 2075, the Moon (Luna) is used as a penal colony by Earth's government, with the inhabitants living in underground cities. Most inhabitants (called "Loonies") are criminals, political exiles, or descendants thereof. The total population is about three million, with men outnumbering women two to one, so that polyandry is the norm. Although Earth's Protector of the Lunar Colonies (called the "Warden") holds power, in practice there is little intervention in the loose Lunar society.
HOLMES IV ("High-Optional, Logical, Multi-Evaluating Supervisor, Mark IV") is the Lunar Authority's master computer, having almost total control of Luna's machinery on the grounds that a single computer is cheaper than (though not as safe as) multiple independent systems.
The story is narrated by Manuel Garcia "Mannie" O'Kelly-Davis, a computer technician who discovers that HOLMES IV has achieved self-awareness and has developed a sense of humor. Mannie names it "Mike" after Mycroft Holmes, brother of Sherlock Holmes, and they become friends.
At the beginning of the story, Mannie, at Mike's request, places a recorder in an anti-Authority meeting. When the authorities raid the gathering, Mannie flees with Wyoming ("Wyoh") Knott, a political agitator, whom he introduces to Mike and with whom he meets his former teacher, the elderly Professor Bernardo de la Paz, who claims that Luna must stop exporting hydroponic wheat to Earth or its limited water resources will be exhausted. In connection with this, Mike calculates that if no prevention occurs, there will be food riots in seven years and cannibalism in nine. Wyoh and the Professor decide to start a revolution, which Mannie is persuaded to join after Mike calculates that it has a 1 in 7 chance of success.
Mannie, Wyoh, and de la Paz thereafter form covert cells, protected by Mike, who adopts the persona of "Adam Selene", leader of the movement, and communicates via the telephone system. Mannie saves the life of Stuart Rene LaJoie, a rich, well-connected, sympathetic tourist, who begins turning public opinion on Earth in favor of Lunar independence. When soldiers brought to quell the mounting unrest rape and kill a local young woman, then kill another who finds her body, rioting erupts. The Loonies overcome military opposition and overthrow the Lunar Authority's Protector, called "the Warden." When Earth tries to reclaim the colony, the revolutionaries plan to use in defense a smaller duplicate of the electromagnetic catapult used to export wheat.
Mike impersonates the Warden in messages to Earth, to give the revolutionaries time to organize their work. Meanwhile, the Professor sets up an "Ad-Hoc Congress" to distract dissenters. When Earth finally learns the truth, Luna declares its independence on July 4, 2076, the 300th anniversary of the United States' Declaration of Independence.
Mannie and the Professor go to Earth to plead Luna's case, where they are received in Agra by the Federated Nations, and embark on a world tour advertising the benefits of a free Luna, while urging various governments to build a catapult to transfer supplies, especially water, to Luna in exchange for grain. Their proposals are rejected and they are imprisoned; but they are freed by Stuart LaJoie and returned, with him, to Luna.
Public opinion on Earth has become fragmented, while on Luna the news of Mannie's arrest and the attempt to bribe him with the appointment of himself as Warden have unified the normally fractious Loonies. An election is held in which Mannie, Wyoh, and the Professor are elected (possibly by the intervention of Mike).
(The title is an acronym for There Ain't No Such Thing As A Free Lunch!)
The Federated Nations on Earth send armies to destroy the Lunar revolution, but these are vanquished, with great loss of life, by the revolutionaries. The rumor is circulated that Mike's alter-ego Adam Selene was among those killed, thus removing the need for him to appear in the flesh.
When Mike launches rocks at sparsely populated locations on Earth, warnings are released to the press detailing the times and locations of the bombings—but disbelieving people, as well as people on religious pilgrimages, travel to the sites and die. As a result, public opinion turns against the fledgling nation.
A second attack destroys Mike's original catapult, but the Loonies have built a secondary smaller one in a secret location, and with Mannie acting as its on-site commander, the Loonies continue to attack Earth until it concedes Luna's independence. Professor Bernardo de la Paz, as leader of the nation, proclaims victory to the gathered crowds, but collapses and dies. Mannie takes control, but he and Wyoh eventually withdraw from politics altogether, and find that the new government falls short of their expectations.
When Mannie tries to speak to Mike afterwards, he finds out that the computer has lost its self-awareness and its human-like qualities.