Like most folklore, the history of the jack-o'-lantern varies a little bit depending on who's telling the story. But all stories involve a clever drunkard that pulls one over on the devil.
Legend has it, in 18th-century Ireland, a foul-mouthed drunk and disreputable miser named Stingy Jack asked the devil to go have a drink with him. The devil obliged and when the bill came, there was that awkward moment that we're all so familiar with.