I've lost weight and kept it off by making sure I don't have sweets and other types of food that I know I'll down in ten minutes. But if I don't have any on hand, I won't eat any.
Ditto for normal food: if I increase the portion of filling, low-calorie foods (think salads and other greens), with a lot of protein, I know I won't have cravings later in the day and feel full longer. But if I buy fatty sausage, with a hefty helping of salty cheese, I'll eat until I explode.
If when I'm hungry I have a choice between "a quick bite" of something very palatable, calorie-rich but nutritionally poor, and a "good" meal, I know I'll make the wrong choice. But if my only choice is the good meal, I'll eat that and stay on track.
This also has a positive feedback effect: after a while, not only did I stop craving various junk foods, but they actually don't register as food anymore when I see them in ads or similar. Which, of course, makes it easier to stick to the "diet". Now, when I'm hungry, I crave "actual" food. The other day I couldn't have lunch on time, and all I could think of was a fat bowl of lettuce with some roasted chicken breast and yogurt. Walking in front of the local McDonald's peddling their latest mystery burger didn't do anything to me.