We have all witnessed it, and many of us have experienced it. A person treats you well 80 percent of the time, but 20 percent of the time they treat you like a foot. Healthy people do not tolerate it and move on quickly. Unhealthy people stick around. "Yes, he forgets my birthday, but he takes me on great weekend getaways sometimes." A person is not entitled to abuse you because they are nice to you. Yet that happens in many toxic platonic, professional, familial, and romantic relationships. Many of us with ailing self-esteems allow people to abuse us because they have been nice to us. Why is that? There are two reasons.
From birth, your brain sets out to wire itself to provide you with the best life possible. Towards this end, the developing brain takes cues from your early environment to determine how the world should be and wires itself to survive in that world. People growing up with parents who were exceptionally loving sometimes and extremely abusive other times believe that that is how the world is supposed to be. Thus, the mindset is: If you are nice to me, you may hurt me. As they rationalized their parents' behavior for emotional survival, they rationalize their significant other's behavior as well. 
"I thought you said the brain's job is to make life as good as possible for the individual," you ask. Not exactly. I said the brain's job is to make life as good as possible for the individual in the context of how his or her brain believes the world should be. That is one part of the puzzle.
The other part of the puzzle involves variable reinforcement, which the reward center of the brain absolutely loves. For clarity, we need to understand the difference between variable and fixed reinforcement. Fixed reinforcement means if you do X you get Y, and thus, do 2X and get 2Y. In a romantic relationship that would translate into the following: Take a date to McDonald's and get to first base. Take a date to Red Lobster and get to second base. Take a date to a five-star restaurant and you get to be dessert. If a person knows exactly what he or she needs to do to get what they want, then they can calculate it mentally. Fixed reinforcement leads to reasonable behavior, because the rules are clear.
Conversely, variable reinforcement, which gives somewhat random rewards to specific behaviors, is a strong trigger for addiction, making relationships with toxic dynamics attractive. With variable reinforcement, the reward is not commensurate to the amount of work done. This is similar to gambling. Put $10 on the craps table, and sometimes you will lose it, and other times you win big. Not knowing when the big wins will come makes the relationship more exciting, and it generates adrenaline. Therefore, if a romantic partner acts loving at times, and withdrawn and distant at others, their partner begins mentally lusting after him or her. Psychological and emotional lust for attention and validation drives toxic relationship dependency more often than sexual lust. 
Dopamine (the brain's happy-dance drug) responds to lust. Dopamine encodes on the anticipation of reward. Like the compulsive gambler anticipating the big payoff, the toxic relationship addict is waiting for the jackpot of love. Not knowing whether that is going to happen or not, causes adrenaline release. Adrenaline is a stimulant. Stimulants are addictive. In terms of neuro chemicals, you can think of dopamine as straight whiskey, adrenaline as beer, and the brain as a boozehound. If it had to choose, the brain would choose dopamine over adrenaline all day, every day. If given the choice though, the brain loves nothing more than a shot of dopamine with an adrenaline back. There is a downward synergy between availability, chance, and lust. Decreased availability of something diminishes our chances of ownership, which increases our perception of its worth, and subsequently our lust for it. [ 3]
Obviously, the brain's love for a shot of dopamine with an adrenaline back is not a life sentence to toxic relationships, since most people do not do this. However, the reason most people do not do this is input from the prefrontal cortex, i.e. rational thought. The dopamine/adrenaline scenario involved in messy-love addiction occurs in the non-thinking, emotional part of the brain. While the prefrontal cortex, in the thinking part of the brain, can influence subcortical structures, it is dependent on serotonin availability. In messy-love junkies, serotonin is usually not available because of life distractions and stress. 
The resounding question is: How do you escape this? Like all things human, it comes down to learning and awareness. You have to be aware that toxic relationships are appealing because that is what your brain expects from the world based on your early experiences. You may have been born an emotional refugee but you do not have to be one for your entire life.
Like any addiction there is one ultimate solution -- find a way to let it go. Toxic relationship addiction is like any other master/slave relationship. It is not the master, who sets the slave free; it is the slave, who lets the master go. You have to stop chasing the big emotional pay off in relationships that can only provide it mercurially. It is a gambler's game. As a crap shooting man's son, I assure you, the trick in winning a crap game is never making a bet that you cannot afford to lose. No one can ever afford to lose his or her heart, spirit, or dignity. Remain fabulous and phenomenal.
The neuroscience of human relationships: Attachment and the developing social brain. Cozolino, Louis New York, NY, US: W W Norton & Co. (2006). x 447 pp.
Substance Use & Misuse, 1986, Vol. 21, No. 9-10 : Pages 1001-1016 Arousal and Sensation-Seeking Components in the General Explanation of Gambling and Gambling Addictions R. I. F. Brown (doi: 10.3109/10826088609077251)
Brain Research Reviews 28 Ž1998. 309-369 (Berridge et al)