If the HOA lien is inferior to the mortgage, then the HOA lien will be foreclosed upon, which is to say, stripped from the property. IF the property sells for more than what is owed to the lender, then the HOA, and others having an interest in the property, can petition to receive the surplus. But, if the property sells for less than what is owed to the lender, there will be no surplus, and the HOA will not receive anything from the sale.
That does not have to be the end of the story. The owner has a contractual obligation to pay the assessments, and the HOA may elect to bring suit against the former homeowner. But, the HOA will be just another unsecured creditor, just like a hospital or medical bill or credit card.