Your body wants to engage in certain behaviors, as they serve a certain purpose. It rewards you for them as such.
Likewise, it punishes you for other behaviors which might bring about obvious harm.
It simply happens to be the case that the system isn't foolproof, and really wasn't adapted to deal with today's world or life circumstances. For that reason, things can often have a tendency to get confused.
A caveman simply wouldn't have had enough food on hand to become obese, for instance. Today, a great many people do, and they wind up losing control of themselves because of it.
The same is true of sex. It is much more widely available today than in our evolutionary past, and seemingly available without consequence (of the immediate variety, anyway).
Just like with food, a lot of people lose control of themselves and wind up getting in trouble for that exact reason.