Why people have affairs will have as many answers as there are people. And does not necessarily mean that there was deep unhappiness in the marriage (accidental infidelity by baby steps isn't uncommon, as the betraying spouse, who would never have dreamed of deliberate adultery slowly gets sucked in by attention and buzz that seems harmless - it's why early signs of an EA are so serious).
But if you have a spouse who says they want to leave, then let them go. You cannot force someone to stay; or rather, even if you force someone to remain present, you'll be left with that gnawing uncertainty that they don't really want you.
Mending a marriage after infidelity is seriously hard, and really quite risky as it means at some stage putting trust back into someone who has shown themself to be all too capable of breaching that trust in secrecy. But if you want to make that attempt, the only start point is both of you agreeing that you want to fix things together. If he is saying he wants to leave, he does not have that view.
In which case, you have little choice but to accept his intention to go. And then let the decision be decisive. He needs to leave, and you need to reduce contact to only what is administratively necessary. This serves a dual purpose - if they've gone for good, you have started the independent track to your new future without waiting around wasting time on 'what ifs', but also, if there is any possibility that they will (even for selfish reasons) decide it is you they want, it's the fastest way to come to that realisation. And from a separate life, you are stronger and better able to decide if you actually want this person back in your life.
Try not to make such a big decision in the immediate emotional turmoil of the crisis period of discovery. Take some healing time (have you RL support?) and as you emerge, engage your head, not just your heart.