The physical act of putting one's tongue into one's cheek once signified contempt. For example, in Tobias Smollett's The Adventures of Roderick Random, which was published in 1748, the eponymous hero takes a coach to Bath, and on the way, apprehends a highwayman. This provokes an altercation with a less brave passenger:
He looked back and pronounced with a faltering voice, 'O! 'tis very well--damn my blood! I shall find a time.' I signified my contempt of him by thrusting my tongue in my cheek, which humbled him so much, that he scarce swore another oath aloud during the whole journey.
The fellow who gave this all-hail thrust his tongue in his cheek to some scapegraces like himself.
It is not clear how Scott intended readers to understand the phrase. The more modern ironic sense appeared in the 1842 poem "The Ingoldsby Legends" by the English clergyman Richard Barham, in which a Frenchman inspects a watch and cries:
'Superbe! Magnifique!' / (with his tongue in his cheek)
The ironic usage originates with the idea of suppressed mirth—biting one's tongue to prevent an outburst of laughter.
... Novelist Sir Walter Scott used 'tongue in cheek' as early as 1828 in 'The Fair Maid of Perth,' but it isn't clear what he meant.