This doesn't always happen, but it has happened often enough to me, so you might think about the possibility in advance and what you would do.
For myself, I have NEVER taken the counter-offer and stayed. When I have had a bad manager, I regarded that as a failure of my manager's manager as well. I didn't think the situation would instantly be fixed, miraculously, if they did fire my boss or move me to a new position in the same company. But that is just my experience. It would be good if others who have had a different and successful experience staying would speak up too, so you can get the full picture of possible outcomes.