According the Wikipedia:
Mencken's The American Language and The Thesaurus of American Slang proclaim that lam, lamister, and "on the lam" — all referring to a hasty departure — were common in thieves' slang before the turn of the Twentieth century. Mencken quotes a newspaper report on the origin of 'lam' which actually traces it indirectly back to Shakespeare's time.
Its origin should be obvious to anyone who runs over several colloquial phrases for leavetaking, such as 'beat it' and 'hit the trail'. The allusion in 'lam' is to 'beat,' and 'beat it' is Old English, meaning 'to leave.' During the period of George Ade's 'Fables in Slang' (1900), cabaret society delight in talking slang, and 'lam' was current. Like many other terms, it went under in the flood of new usages of those days, but was preserved in criminal slang. A quarter of a century later it reappeared.
Mencken also quotes a story from the New York Herald Tribune in 1938 which reported that "one of the oldest police officers in New York said that he had heard "on the lam" thirty years ago."