In 1862, during the American Civil War, a trio of bounty hunters attempt to kill fugitive Mexican bandit Tuco Ramírez. Tuco shoots the three bounty hunters and escapes on horseback. Elsewhere, mercenary Angel Eyes interrogates former Confederate soldier Stevens, whom Angel Eyes is contracted to kill, about Jackson, a fugitive who stole a cache of Confederate gold. Angel Eyes makes Stevens tell him the new name Jackson is using: Bill Carson. Stevens offers Angel Eyes $1,000 to kill Baker, Angel Eyes’s employer. Angel Eyes accepts the new commission, but also kills Stevens as he leaves, fulfilling his contract with Baker. He then returns to Baker, collects his fee for killing Stevens, and then, shoots Baker, fulfilling his commission from Stevens. Meanwhile, Tuco is rescued from three bounty hunters by a nameless drifter to whom Tuco refers as “Blondie”, who delivers him to the local sheriff to collect his $2,000 bounty. As Tuco is about to be hanged, Blondie severs Tuco’s noose by shooting it, and sets him free. The two escape on horseback and split the bounty in a lucrative money-making scheme. They repeat the process in another town for more reward money. Blondie grows weary of Tuco’s complaints, and abandons him without horse or water in the desert. Tuco manages to walk to a village, and then tracks Blondie to a town occupied by Confederate troops. Tuco holds Blondie at gunpoint, planning to force him to hang himself, but Union forces shell the town, allowing Blondie to escape.
Following an arduous search, Tuco recaptures Blondie and force-marches him across a desert until Blondie collapses from dehydration. As Tuco prepares to shoot him, he sees a runaway carriage. Inside are several dead Confederate soldiers and a near-death Bill Carson, who promises Tuco $200,000 in Confederate gold, buried in a grave in Sad Hill Cemetery. Tuco demands to know the name on the grave, but Carson collapses from thirst before answering. When Tuco returns with water, Carson has died and Blondie, slumped next to him, reveals that Carson recovered and told him the name on the grave before dying. Tuco, who now has strong motivation to keep Blondie alive, gives him water and takes him to a nearby frontier mission, where his brother is the Abbot, to recover.
After Blondie’s recovery, the two leave in Confederate uniforms from Carson’s carriage, only to be captured by Union soldiers and remanded to the prisoner of war camp of Batterville. At roll call, Tuco answers for “Bill Carson”, getting the attention of Angel Eyes, now a disguised Union sergeant at the camp. Angel Eyes tortures Tuco, who reveals the name of the cemetery, but confesses that only Blondie knows the name on the grave. Realizing that Blondie will not yield to torture, Angel Eyes offers him an equal share of the gold and a partnership. Blondie agrees and rides out with Angel Eyes and his gang. Tuco is packed on a train to be executed, but escapes.
Blondie, Angel Eyes, and his henchmen arrive in an evacuated town. Tuco, having fled to the same town, takes a bath in a ramshackle hotel and is surprised by Elam, one of the three bounty hunters who tried to kill him. Tuco shoots Elam, causing Blondie to investigate the gunshots. He finds Tuco, and they agree to resume their old partnership. The pair kill Angel Eyes’s men, but discover that Angel Eyes himself has escaped.
Tuco and Blondie travel toward Sad Hill, but their way is blocked by Union troops on one side of a strategic bridge, with Confederates on the other. Blondie decides to destroy the bridge to disperse the two armies to allow access to the cemetery. As they wire the bridge with explosives, Tuco suggests they share information, in case one person dies before he can help the other. Tuco reveals the name of the cemetery, while Blondie says “Arch Stanton” is the name on the grave. After the bridge explodes, the armies disperse, and Tuco steals a horse and rides to Sad Hill to claim the gold for himself. He finds Arch Stanton’s grave and begins digging. Blondie arrives and encourages him at gunpoint to continue. A moment later, Angel Eyes surprises them both. Blondie opens Stanton’s grave, revealing only a skeleton, no gold. Blondie states that he lied about the name on the grave, and offers to write the real name of the grave on a rock. Placing it face-down in the courtyard of the cemetery, he challenges Tuco and Angel Eyes to a three-way duel.
The trio stare each other down. Everyone draws, and Blondie shoots and kills Angel Eyes, while Tuco discovers that his own gun was unloaded by Blondie the night before. Blondie reveals that the gold is actually in the grave beside Arch Stanton’s, marked “Unknown”. Tuco is initially elated to find bags of gold, but Blondie holds him at gunpoint and orders him into a hangman’s noose beneath a tree. Blondie binds Tuco’s hands and forces him to stand balanced precariously atop an unsteady grave marker while he takes half the gold and rides away. As Tuco screams for mercy, Blondie returns into sight. Blondie severs the rope with a rifle shot, dropping Tuco, alive but tied up, onto his share of the gold. Tuco curses loudly while Blondie rides off into the horizon.