Once the foreclosure happens, the bank (owner) is obligated to pay the HOA. In most cases they won’t. The HOA assessments will accumulate and become a junior lien on the property (behind RE taxes, recorded trusts deeds). As you stated, if there is any equity in the home, they might eventually get paid when the home is sold but that could be years (and tens of thousands of dollars) later. The HOA can go after the former owner personally but that is usually a waste of time.
My building has $48,000 in HOA receivables. They also just wrote off $6,000 on one unit that is uncollectible because the former owner didn’t pay for several months before they were foreclosed on.