First in time is first in line … and once bidding goes past the full debt due on the loan going to sale, any overage goes to the next in line (recorded debt in good standing) until they are fully paid and then to the next and so on and so on. On occasions (rare) there is also some overage that goes to the delinquent homeowner who probably should have attempted to sell the property vs let it go to foreclosure.
You can find the full amount due in the notice of trustee sale that has been recorded. It will show as the published bid amt and will be "as of" the date of the NTS. Amount will grow until loan is sold at auction. If you want to know the original loan amount, it will be referenced in the NTS by doc number and recording date. Easy to find who made the loan and the amt. Foreclosure Radar also publishes much of this data for subscribers.
Most mortgages have a payoff requirement once title changes to a new owner. Some lenders will allow you to bring the loan current (paying the back due amt) and continue to make payments (effectively stepping into the shoe of the previous borrower) … but you should be prepared to pay off the full amount due. Best to be proactive and contact the 1st lender. That way you can get clear title.