This phrase clearly alludes to the facial expression created by putting one's tongue in one's cheek. This induces a wink (go on - try it), which has long been an indication that what is being said is to be taken with a pinch of salt. It may have been used to suppress laughter. 'Tongue in cheek' is the antithesis of the later phrase - 'with a straight face'.
The term first appeared in print in 'The Fair Maid of Perth', by that inveterate coiner of phrases, Sir Walter Scott, 1828:
"The fellow who gave this all-hail thrust his tongue in his cheek to some scapegraces like himself."
It isn't entirely clear that Scott was referring to the ironic use of the expression. A later citation from Richard Barham's The Ingoldsby Legends, 1845 is unambiguous though:
He fell to admiring his friend's English watch. He examined the face, And the back of the case, And the young Lady's portrait there, done on enamel, he Saw by the likeness was one of the family; Cried 'Superbe! Magnifique!' (With his tongue in his cheek) Then he open'd the case, just to take a peep in it, and Seized the occasion to pop back the minute hand.
- (idiomatic) not intended seriously; jocular or humorous
- He gave a tongue-in-cheek explanation of why the sky was blue, offering a theory about some primordial discount on light blue paint.
- See also Wikisaurus:witty