Perhaps I can help answer this because I "had an affair" with a married man with children when I was a lot younger than I am now.
Firstly it's one of my biggest regrets and I would never EVER do it again. I'm disgusted by it to be honest now that I look back.
Secondly, I did it because of a few things.
He gave me attention when I was having problems in my life, he approached me not me approaching him. I wasn't interested at first but he kept showing up and "helping me" at work and was extremely supportive so I started to become attracted to what I felt I wasn't getting elsewhere I suppose.
I was selfish and only thinking about myself and I could tell myself that it was his choice to cheat and I was nothing to do with it.
I had no idea what was going on with me emotionally, these days there are a lot of resources out there about trauma and healing but back then there weren't and I was clueless about my own state of mind and just doing things to "fill a void" which were selfish, wrong and asshole-ish.
It's the same reason anyone cheats, they do it because of themselves and what is going on with them and the fact they don't have enough emotional insight or moral standing to NOT do that, and at the time neither did I.
There was also the fact I lived with a perpetual fantasy mindset about romance and true love and any attention from a man must mean those things are real. I do not have that mindset nowadays.
I'd also add that whilst there is no excuse, men can be very manipulative and they will tell you that their wife is a horrible awful person and go for the victim mentality. No excuse though but at the time I believed it.