It's awful to say it, but there is no real way to stop what's coming. The best thing foreign governments could do would be to open up their asylum numbers and try to help any Afghans who want to leave. Long term, the only real answer is to try to coerce the new regime and its backers into better behaviour.
The problem is that 'the Taliban' are not one unitary actor, with one powerful leadership. They are many different factions with different leaders and different backers, they are semi-united right now against the current government but if it falls we will likely see a return to the early 1990s civil war situation.
That will make it very difficult to coerce or negotiate with anyone. It will be a mess.
I feel terrible for the women of Afghanistan and I wish things were different, but women all over the world are suffering like this and we can't occupy every country indefinitely. I don't think anything will change until the whole system changes, until more women are in power, until this era of geopolitical bullshit is over, etc etc.