Z-4195, or "Z" for short, is a neurotic and pessimistic worker ant who longs to express himself. Z falls in love with the ant colony's princess, Bala, when she visits a bar to escape her suffocating royal life.
The ant colony declares war on an encroaching termite colony and a large force of soldier ants is mobilized for an attack. To see Bala again, Z exchanges places with his soldier friend Weaver and joins the army, where he befriends staff sergeant Barbatus. Z is unaware that the army's leader and Bala's fianc� General Mandible is secretly sending the soldiers loyal to the Queen Ant to die so he can stage a coup d'�tat. At the base of a tree near nightfall, Z realizes he is marching into battle. Everyone except Z is killed by acid-shooting termite defenders. Before dying, Barbatus tells Z to think for himself instead of following orders.
Z returns home and is hailed as a war hero (despite not doing anything and left traumatized). Secretly irate, Mandible congratulates him and introduces him to the Queen. There he meets Bala, who eventually recognizes him as a worker. Z panics and pretends to take Bala hostage, causing him and Bala to fall out of the anthill via a garbage chute. Z decides to search for Insectopia, a legendary insect paradise. Bala attempts to return to the colony, but quickly rejoins Z after encountering a praying mantis.
News of the incident spreads through the colony and Z's act of individuality inspires the workers and some soldier ants, halting productivity. To gain control, Mandible publicly portrays Z as a self-centered war criminal, promotes the glory of conformity, and promises them a better life through the reward of completing a "Mega Tunnel" planned by himself. However, Mandible's second-in-command Colonel Cutter becomes concerned about Mandible's plans.
Z and Bala come upon a human picnic laid out on a cloth, which they take for Insectopia, but they are baffled by the wrappings on the food. A married couple of liberal wasps befriend them, though with much condescension, and they all start to break into the food; but they are disrupted by being trampled by giant sneakers and swatted with a fly swatter. Z rescues Bala from the sneaker treads, but the female wasp is killed by the swatter, leaving the male wasp heartbroken. He encourages the ants to move on.
They at last find Insectopia, a human waste bin overfilled with decaying food, where Bala begins to reciprocate Z's feelings. After interrogating Weaver, Mandible learns that Z is looking for Insectopia and sends Cutter to retrieve Bala and kill Z. That night, Cutter arrives in Insectopia and forcibly flies Bala back to the colony. Z finds them gone; seeing his desperation, the grieving wasp widower generously flies him back to the colony.
When Z arrives, he encounters soldiers who forcibly direct him toward the Mega Tunnel. Along the way, he finds Bala held captive in Mandible's office. After he frees her, they both discover that Mandible's "Mega Tunnel" leads straight to the puddle next to Insectopia which Mandible will use to drown the Queen Ant and workers at the opening ceremony. Bala warns the ants at the ceremony, while Z goes to the tunnel exit to stop the workers but fails, and the water leaks in. Z and Bala unify the workers into building a towering ladder of themselves towards the surface as the water rises.