In my case, it was complicated. I was able to grasp my why's one piece at a time and try to piece them together, and it took me years of therapy and introspection just to get to the point where my head finally released from my ass. Once that happened, everything just about fell into place all at once. Part of what I learned was that the affair itself was really part of a much larger and pre-existing problem, which was my mental health overall. The affair was an outcome, but certainly not the only one. It did however, give me something to focus on, rather than the chaotic mess that existed.
Imagine for a moment that instead of an affair, your house had burned down. And then the question becomes, why did my house burn down? For me, it turned out that it wasn't due to something direct, such as leaving the stove burner on, or falling asleep with a lit cigarette, or even an arsonist. My house burned down because there was a forest fire, and anything that the fire got close enough to burst into flames, understand? So it was less about my particular house burning down, and more about the fact that the forest fire burned everything it touched. In this example, my life was the forest fire, and our marriage was the house, and our house (marriage) burned down because the forest fire (me) was at the center of it. I was a forest fire because I had no control over my life, no healthy boundaries, and my ability to love myself and show love and empathy towards others was broken.
You can't have boundaries when you don't even know who you are. Boundaries are born from our gut, and our conscience builds and drives them. When someone from outside the marriage tempts us, it is our boundaries that show up to prevent us from doing the wrong thing. In a healthy personality, our self-respect would kick in, and our head would say, "I'm not going to lower and debase myself in such a disgusting and awful way. No way am I going to be a slimeball like that." But for folks like you and I, those boundaries didn't exist or didn't work. The answer to "why did I cheat" lies in the answer to "why didn't I love or respect myself enough to not be someone who cheats?".
For me, that lack of self was really the key to everything. Since I did not, and could not, love myself, there was nothing to create a boundary from. More importantly however, since I could not love myself, I needed other people to do it for me. I needed other people to love me, tell me I'm great, make me feel special and valued and motivated, etc. As long as I had a steady flow of love from others coming in, I could maintain a false sense of self-respect. I fooled myself into thinking that my limits and boundaries existed. In truth, they only existed as long as external validation and love was available to me. When that flow of external love slowed or stopped however, so did my ability to have boundaries. It's the kind of life that an addict experiences, always chasing that high, always needing something external to help us get through our lives.
In my case, my wife had taken a job that was over an hour from our home, and her employer insisted that she live locally, so she had rented an apartment near work, and came home on weekends to visit. Prior to this, I had my wife's attention 100% of the time. When she wasn't around however, I fell apart. My self-hate and self-loathing kicked in, and so my boundaries went out the door entirely. I didn't understand this at all at the time however, so my brain just made up stories, excuses and justifications that would help explain the emptiness and lonliness that I was experiencing. My brain made up stories that were complete fiction, but I was just so broken, and so very, very needy, that I believed those stories even though they made no sense. My head "made" them make sense, because it needed it needed external love to survive, and by believing my own lies, I was able to throw any boundaries and self-respect I had out the door. I told myself that she no longer loved me, that she was probably just waiting to leave me, that I was a "single dad" and being taken advantage of, and so on. I didn't go looking for an affair, but when it was offered to me, it was like offering a liquor store to an alcoholic. Ethics went out the window, and my brain happily filled in the blanks as needed for the affair to continue.
That's why figuring this stuff out is so tough sometimes. Nobody with an ounce of self-respect has an affair. They wouldn't do that to someone else, and they wouldn't do it to themselves. That being the case, you then need to figure out why you had no self-respect or boundaries, and that... is where the forest fire comes in. At the end of the day, an affair is often less about "why did I cheat?" and is more about "how the hell did I allow my life come to a point where I couldn't respect myself enough to not be that kind of person?" Which is often followed by the question of, "What needs did I have that were unfulfilled, why did I have those needs, and how did the affair attempt to fulfill those needs?". That's where the "why" exists. For most if not all of us, the ultimate "why" is because we needed external validation for some reason. That's the easy part. The hard part is figuring out the "whys" of how you came to be that person. And that will be different for each of us.
Tread carefully, tread slowly, in your search. When something gets uncomfortable, you have to lean into it and suss out the truth. Those lies we told ourselves become our truth, our narrative, and so when we go to examine them, they can fool us into thinking they are real and true. For example, as I said, I told myself stories about how my wife no longer loved me, etc. When I think back to the affair, those were the thoughts in my head, so they seem "real" and reasonable because that's how they felt at the time. Now that I know better, when I think back, I have make a manual effort to correct those thoughts, and label them as "not true". Then I put the truth in where it belongs. ("Remember, you were supposed to move out to be with her after our daughter graduated. Remember how she called every day, and said she missed us, and took every opportunity to come home? You weren't unloved, you were just lonely without her.") Once the bullshit gets replaced with the truth, it's easier to start making sense of it, for both the WS and the BS.
I wish you luck. I'm sorry, I know this is a painful process. But if there is to be any hope of R, then it has to happen, so keep digging.