The phrase comes from Hamlet, Act 3, Scene 1 (the "To be, or not to be" soliloquy). Hamlet says:
"To sleep, perchance to dream: ay, there's the rub."
Meaning: The "rub" is the crucial difficulty or obstacle—the uncertainty of what dreams (afterlife) may come, which makes him hesitate to commit suicide.
Today, "there's the rub" is used generally to denote a catch or complicating factor—the hidden difficulty that changes everything.