Sergio Leone had seen the Tabernas Desert in Almería, Spain in 1959. In 1964 he decided to film «A Fistful of Dollars» there (on a budget of only $200,000). He then got «A Few Dollars More» ($600,000 to be precise) to film his next movie. He finally he got $1,200,000 to film «The Good, The Bad, and The Ugly» (titled «The Good, The Ugly, and The Bad» in Spain, though I don’t think it sounds better). Leone’s movies transformed the Western, with Ennio Morricone’s music becoming The classic Western soundtrack.
To track all filming locations for these movies, see this thorough map guide.
For the final movie’s blowing-up-the-bridge scene and the final Sad Hill Cemetary scene, locations near Burgos were used. In 2014, some local fans of Leone’s masterpiece got together and decided to uncover the turf that had grown over the stones in the center of the cemetery. They became an association and kept at it until they had re-created the whole cemetery, with the help of crowdfunding donations (for 15€, you get your name on a cemetary cross). They topped it off by putting on a 50th anniversary screening, complete with a documentary of the whole re-creation project and a dedication by Eastwood himself (and a lot more words by the Metallica lead singer, who for 30 years starts his shows off with the movie finale). Donations are still coming in and the cemetery is up to 4000 names. Meanwhile, the three town scenes in Almeria remain as «Mini Hollywood» tourist attractions ever since Leone finished filming.
The documentary describes how Leone tried to used locals as extras in the American Civil War scenes, but after the resulting chaos decided to use the more disciplined soldiers from the local Burgos military barracks. Franco’s military also helped out with setting up the explosives to blow up the bridge. Unfortunately, when someone was told the signal to blow it up would be to wave a flag, the other replied, «you mean like this?». The bridge blew up without cameras rolling, Leone was livid, and the actors were sent down to Almeria to film scenes while the bridge was re-built.