The bank that held the earliest interest in the loan (whoever you signed with first) will be able to collect on the sale first, then the other holder second.
The first mortgage gets paid off first, the second mortgage with any money left after paying off the first mortgage. The lender will then sue the borrower for the difference between the balance and what they received.