The phrase was coined by Don Quixote, a 17th century novel by Miguel de Cervantes. In the story, the protagonist sees a line of windmills, and believing they are giants, attempts to joust at them. He justifies this by declaring it a “righteous war”, claiming that “the removal of so foul a brood from the face of the earth is a service God will bless”.
Does this happen to remind you of claims made by a certain infamous political leader in the 20th century, by any chance? Coincidently, this idiom played an important role during World War I: Read the Gems of the Archive article, Tilting at Windmills, to uncover how this piece of classic literature was used in war propaganda, and the irony of its use to represent the futility of war on both sides.