The use of words to express the opposite of their literal meaning is known as irony, more specifically verbal irony. A speaker says one thing while intending the audience to understand a contrasting meaning, often for humor, emphasis, criticism, or dramatic effect.
The term traces back to the ancient Greek eirōneía, associated with the eirōn, a character who pretended to know less than he did in order to outwit a boastful opponent. Irony later became a recognized rhetorical device in classical discussions of language and entered English usage in the 16th century through French and Latin forms.
A common mix-up is sarcasm. Sarcasm often uses verbal irony, but it usually adds mockery, ridicule, or a cutting tone; not every ironic statement is sarcastic. For example, saying “What lovely weather” during a downpour can be ironic without targeting a person.
Irony also has broader forms. Dramatic irony occurs when an audience knows information that a character does not, while situational irony describes an outcome that sharply contrasts with expectations. These are related to, but distinct from, the direct wordplay of verbal irony.