What to do: Call a lawyer. Once you've paid the general contractor in full, your contract should indemnify you against any sub coming after you for money, says Ty Laurie, a construction lawyer at DLA Piper, in Chicago. If yours was a handshake deal, you may have to fend off the lien in court. Even if you win, that could cost thousands in legal fees, so it might be worth negotiating with the sub instead.
How to keep this from happening to you: Have your lawyer review the contract up front, and don't make any payments to the GC until receiving lien waivers from all the subs. Or, for a couple of hundred bucks, a title company will hold the money in escrow and disburse it to the GC or the subcontractors.