oblomov Abedelia has explained it very well, but there are all sorts of variations of it. It can also be that the marriage is happy, but one of the people within it is feeling unhappy about other aspects of life - they can be feeling old and out of potential due to hitting a ceiling at work, or they may have endured a bereavement, or they might be feeling mildly depressed.
Let's work with a male example. Along comes someone who flatters him that he is the most dazzling person in the planet. She laughs at all his lame jokes and tells him how insightful and clever he is. Plus of course, that he is sex on legs.
Now normally because the marriage is perfectly happy and he knows there is absolutely no justification for having an affair, the H very subtly (and often totally sub-consciously) starts to create one. So he starts behaving badly at home and that produces a perfectly natural (bad) reaction in his partner, who complains about his laziness, his withdrawal etc. from aspects of family life. In effect, what he is doing is trying to reduce the connection, because since he's an essentially good person, he can't bring himself to be unfaithful when things are good.
By the time he finally says "yes" to an affair, he has become totally addicted to the feelings the new love interest has given him. That is why the "stop" switch isn't pressed. Even then, his justifications can be really banal - and are often not justifications but perceived consequences, or lack of them. Things like - this is just a bit of fun, no-one need ever know, the OW is married and has a lot to lose, I'm never going to fall in love with OW, she won't be a threat to the marriage etc.
For a man in mid-life, it can also be "If not now - never" and also "I've been a good man all my life, I've never done anything risky, don't I deserve an adventure?"
People like this are rarely compartmentalisers and aren't actually very "good" at deception. They can't cope with the stress and behave erratically at home and at work. They provoke arguments with their baffled partner, because if she is nice to him, it makes the guilt worse.
Some of these people actually feel relief when the affair ends and can give up the OW/OM easily, because their addiction wasn't to him or her, but the feelings everyone gets in the first flush of a relationship. It was a fantasy all along and not based on reality at all.
Very often, the person would never have got involved with the affair partner in a normal relationship, had they been single. This person just happened to pop up at a time when they were feeling particularly low.
It can also happen in happy marriages, but have an unhappier ending if it starts with a friendship with another person, often at work. This is more slow-burning and often catches a faithful person completely unawares. Because of the closeness of working together on projects etc., the colleagues spend more time together than with their spouses.
Before they know what's happening, they are sharing confidences about their home lives, dreams and aspirations. Because they don't live together and have to put up with eachother's moods, habits or competing priorities such as DCs, they can give eachother undivided attention. Soon, they are complaining to eachother about their inattentive spouses, their lives of drudgery etc., exaggerating these for a bit of soothing sympathy. They start telling eachother that they really understand eachother in a ways their spouses don't.
By this time, because behaviour has got markedly worse at home, their spouses certainly don't understand them, but before the friendship started, everything at home had actually been fine and perfectly happy.
Unfortunately too, having created the unhappiness at home to justify an affair, some people start to believe their own press and reason that they couldn't possibly be having an affair if they were really happy and so they make terrible decisions that the affair partner must be their soul mate. They leave their spouse, only to find later on that they have lost someone far more precious than the affair partner, but by now it's too late.
Do read Not Just Friends, which talks about the boundaries that are sometimes imperceptibly crossed by people who are otherwise good, kind, moral souls.
And it is because these people are normally kind, decent, trustworthy folk who pay their taxes, perform good deeds and have been model spouses, that their trusting partners are blind-sided and cannot believe that an affair is possible, even if their instincts are telling them something is wrong.
It is one of the most dangerous myths of all that people in happy marriages don't have affairs. Or that only "bad people" are unfaithful.