Jig is an old term for a lively dance, and in the Elizabethan era the word also became slang for a practical joke or a trick. This idiom derives from this obsolete slang word. 
the jig is up (simple past the jig was up)
- (US, idiomatic) An expression used to mean "We have been caught out and have no defense", or if spoken to a person who has just been found out as the perpetrator of an offense, where it means "You've been discovered".
1753, The Skipper:
- We knew then the jig was up, and it was no grin matter for us.
1833, Seba Smith (as Jack Downing), The life and writings of Major Jack Downing, of Downingville, away down East in the State of Maine, Lilly; Wait; Colman & Holden, page 176:
- When I first told 'em how the jig was up with us, that the British were going to have the land, without any fighting about it, I never see fellows so mad before in my life, unless it was Major Eaton at Washington when he sot out to flog Mr. Ingham.
1920, Champ Clark, My Quarter Century of American Politics, Harper & brothers, page 96:
- After I had returned home in the spring of 1893 from Washington, where I saw so many gray-haired men who had held high elective office begging for the crumbs from Cleveland's table, I gave my wife an account of what I observed, and told her that when the jig was up for me I would hasten back to Missouri to begin the practice of law once more and be a man among men.
2007, Mary Newport, in Jerry Newport and Mary Newport, Mozart and the Whale: An Asperger's Love Story, Simon and Schuster, ISBN 074327282X, page 248:
- The universe works in strange ways: just when you think the jig is up, you get a second chance.
- ^ Robert Hendrickson (1997) Encyclopedia of Word and Phrase Origins, New York: Facts on File