Fill Holes floods the background from the image border inward, marking every background pixel the flood can reach. When the flood stops, any background pixel left unmarked is unreachable from the border by definition, which means foreground encloses it. The engine converts those pixels to foreground.
The method also explains why, with Allow fill on border off, edge-touching holes stay open. The flood reaches them on its first move, and once they are marked reachable, the engine leaves them as they are.