The mortgage lender first has the title searched and confirms prior to recordation of their instrument that they will have the lien priority they desire. A smart first lien lender will insist upon a title insurance loan policy to guarantee that position.
If the search reveals a lien in a superior position and the parties do not intend to satisfy that lien, they can negotiate for a postponement or subordination agreement. This agreement may or may not require a payment from the borrower for the privilege of moving the lien priority. The borrower should start negotiations for a postponement as soon as they are aware of the issue. Some lenders require an appraisal and the process might take time. Once approved, the postponement is recorded prior to the recordation of the new mortgage instrument and it effectively gives public notice that the new mortgage instrument has priority over the other lien even though the original recording date of the older lien is earlier.