There are three factors that create a habit. Cue, routine, and reward.
A simple example is a rat put inside a maze, with a hidden piece of chocolate. On a clicking sound, the door to the maze opens. The rat smells the chocolate, wanders around the maze, takes a lot of wrong turns, but eventually finds the chocolate.
The experiment is repeated several times, with the chocolate placed in the same location. The rat learns where the chocolate is and finds a faster and more direct way to the chocolate. Eventually, it becomes automatic - when the rat hears a click, its brain follows the path to chocolate. Even when the chocolate (and the smell of it) is removed, the rat always follows the same path out of habit.
Cue: The clicking sound when the maze is opened.
Routine: Following a path through the maze.
Source: Duhigg, Charles. The Power of Habit: Why We Do What We Do in Life and Business. Random House, 2012.
Example bad habit
A common example is the Facebook habit. Students will often get tired/bored of reading and enter Facebook for some relaxation. Before he realizes it, he's failing class yet constantly logged into multiple Facebook games.
Cue: This can be anything. Being bored at lunch. Arriving home from work. Finishing a difficult task, like studying a chapter or emptying the office tray. More often, the cue failing to finish a difficult task and looking for some relaxation.
Routine: The person logs into Facebook, and stays there until he gets the reward he wants.
Reward: He gets in touch with someone he likes. He collects some strawberries on Farmville. He sees a pretty girl's new picture. He wins an argument with a friend. He chats with enough friends to have an empty inbox.
For some, the reward is brief - click notifications, find nothing, then log off.
For others, they may be looking for a post from someone they are in love with, so they'll constantly refresh until that person posts. This is what causes the more dangerous forms of Facebook addiction.
Example good habit
I assume most people are familiar with the Pomodoro technique. I'll take the classical physical Pomodoro as an example.
Cue: Physically winding the Pomodoro and getting a ticking sound.
Routine: Full focus on a particular task.
Reward: The ring at the end of the Pomodoro, and being able to add one point to the number of Pomodoros accomplished. No longer hearing a ticking sound.
As long as the cue and reward remain the same, people will do same routine, which is doing a task with full focus. That's why the Pomodoro technique has proven to be so effective - it's fully ingrained into the habit system.
How to modify habits
The routine does not switch off until the person has reached the reward. The cue will often launch the habit, no matter how much willpower you put into avoiding the habit. You'll experience cravings if you don't get your reward. In fact, you may even get angry if you don't get your reward and not understand why.
What most people hate about habits is the routine involved. If it's a time-wasting routine, like Facebook, accelerate yourself towards the reward.
Sometimes the reward is difficult to reach for a bad habit. So learn to replace it with an easier to obtain reward. Play a less addictive game. Train yourself to be excited just to read all your notifications. Instead of stalking someone who rarely posts, stalk someone else who posts more often.
Sometimes the reward itself is bad, so change the reward. Instead of eating bacon for breakfast, form a habit of eating toast or low fat beef jerkies.
Actually changing your routine and rewards will require some willpower. But using willpower to simply suppress your routine doesn't cure the cravings. The Seinfeld Productivity method works, because it displaces the reward with something very simple.
Here's a flowchart that shows this better.