Every habit consists of three components: cue, routine, reward. Something triggers the behavior, you perform the behavior, and you reap some reward. Rinse, repeat.
For example, a lot of people eat food - especially sweet food - to deal with stress. The cue that triggers the behavior is stress, the behavior itself is eating food, and the reward is the taste of that food. Repeating this enough times causes the person to get fat.
Knowing this, it is also possible to engineer new habits. The way you do this is by starting from the end of the loop - the reward - and work your way backwards. For example, the reward (your motivation) for working out may be to become more attractive. From there, you may decide that going to the gym would help you lose weight, gain muscle and become more attractive, so that would be your routine. And the last part would be to figure out a cue, something that would trigger the routine. This works best as something that will remind you of the reward, such as looking at pictures of hot women.
After you engineer the habit, you can also engineer your life around each component. For example, you can put up posters of hot women on your bedroom wall so that even if you come home tired, the pictures would remind you of your motivation and trigger the gym routine of "put on clothes, go to the gym, work out." You can choose to live within a few blocks of said gym (or build a home gym) so that executing the routine after it is triggered by the cue is as easy as possible. Basically the formula for making sure you continue the habit is: maximize cues + minimize the effort for the routine = maximize rewards. (Side note: this also works the other way around. If you're trying to quit smoking, keeping your cigarettes as far away as possible, such as on one of the top shelves in the kitchen, helps quite a bit.)
The part that takes willpower is executing the routine. That's why willpower is so important, especially at the beginning. The further away you live from the gym, the more willpower it will require to actually go. In contrast, the stronger your motivation, the less willpower the routine will take to execute.
Of course, there's quite a bit more to be said about the topic. But that's the psychological aspect of it.