Here goes: the phrase first appeared in The Ingoldsby Legends by Richard Harris Barham, a clergyman, around 1840 and referred to gentle humour, someone gently poking fun by his comment. That's what Google says anway.
It usually means jokingly or insincerely nowadays. When it was first used in written English - by Tobias Smollett in his novel, Roderick Random, published in 1748 - it suggested some stronger emotion. He wrote: "I signified my contempt of him by thrusting my tongue in my cheek." It can also suggest that one is making an effort not to laugh. The actual form �tongue in cheek' - ie these exact words in that order - did not appear until the 1930s and the hyphenated version �tongue-in-cheek' - not until the 1950s.