The film tells the story of three men who pursue, often at the expense of others, information about the location of a buried treasure of coins. The first character introduced in the movie is Tuco Benedicto Pacifico Juan Maria Ramirez (the Ugly) - called Tuco - (Eli Wallach), who has a bounty on his head for numerous crimes. Tuco has a partnership with Blondie (The Good, played by Clint Eastwood) in which the latter turns him in for the reward money which the two then split after Blondie saves Tuco from hanging at the last moment. Meanwhile, a third character called Angel Eyes (Lee Van Cleef, playing the Bad) has learned of a hidden trunk of gold owned by a Confederate soldier named Bill Carson. He sets off to find the gold.
Soon, Blondie grows tired of his relationship with Tuco, and leaves Tuco in the desert with no water. Tuco survives and is intent on exacting revenge on his former partner. He finds Blondie, and turns the tables by planning to abandon him in the desert. However, before Tuco can complete his torture in the New Mexico desert, a runaway stagecoach full of dead and dying Confederate soldiers appears. Bill Carson, the man with knowledge of the whereabouts of the gold, dying from thirst, persuades Tuco to get him a drink by disclosing the name of the graveyard where the loot is located. As Tuco goes for the water, Carson dies, but not before revealing the name on the grave to Blondie.
Dressed in the uniforms of the dead soldiers, Tuco takes Blondie, near death, to a local Catholic mission run by his brother, a priest. While Blondie recovers, Tuco and his brother (Luigi Pistilli) confront each other about the mistakes each has made in life. After leaving the mission, the two, still impersonating Confederate soldiers are captured and taken to a Union prison camp. Angel Eyes has followed the trail of Bill Carson to the prison camp and is posing as a Union Sergeant.
Angel Eyes and his colleague Wallace beat and torture Tuco until he reveals the location of the cemetery. When Angel Eyes learns that only Blondie knows the name, he changes tactics. He proposes a partnership, and accompanied by five or six other killers, they leave to find the coins. Tuco escapes while being transported from the camp by train, in the process killing Wallace. At the nearest town, Tuco encounters a bounty hunter (Al Mulock) he had wounded at the beginning of the film, who seeks his revenge. As Tuco shoots the bounty hunter, Blondie, who is in the same town with Angel Eyes, recognizes the sound of Tuco's gun, seeks him out, and he and Tuco resume their old partnership. Together they kill Angel Eyes' gunmen along the main street, but Angel Eyes himself escapes.
Tuco and Blondie stumble on a battle between the Union and the Confederates, fighting for a bridge of questionable strategic value. Since the cemetery is on the other side of the bridge, they decide to destroy it and force the soldiers go somewhere else to fight. While they are setting up the dynamite, Tuco reveals that the cemetery is called Sad Hill and Blondie reveals that the coins are buried in a grave marked by the name of Arch Stanton.
On the other side of the river Tuco deserts Blondie by horseback and finally enters the nearby graveyard.
Tuco frantically searches around the graveyard for the grave of Arch Stanton. Eventually Tuco finds it, but before he can begin digging he's held at gunpoint by Blondie, who in turn is held at gunpoint by Angel Eyes, who has finally caught up to both of them. However, Blondie reveals that Arch Stanton's grave contains only a decomposing corpse.
Blondie then leads the three of them into an empty patch of land in the middle of the cemetery. He writes the name of the real grave under a stone which he places in the center.
At the conclusion of a three-way shootout, Blondie shoots Angel Eyes and Tuco finds his gun empty, having been unloaded the previous night by Blondie. Blondie then reveals that the real location of the coins is a grave marked "Unknown" right next to Arch Stanton. Tuco digs up the loot from the grave only to find himself once again staring down the barrel of Blondie's gun, who now holds a noose in his hand. After placing Tuco into the noose, fastening it to a nearby tree and making Tuco stand on the unstable wooden cross of one of the graves, Blondie takes half the coins and rides away while Tuco cries for help. In a dramatic twist, Blondie turns around to shoot the rope above Tuco's head, as he used to do in their times of partnership, freeing him one last time before riding off as Tuco screams in rage.