Self-control—or the ability to subdue one's impulses, emotions, and behaviors in order to achieve longer-term goals—is what separates modern people from their ancient ancestors and the rest of the animal kingdom. Self-control is primarily rooted in the pre-frontal cortex, which is significantly larger in humans than in other mammals with similar brains.
Thanks to the pre-frontal cortex, rather than immediately responding to every impulse as it arises, individuals can plan, evaluate alternative actions, and ideally avoid doing things they'll later regret.
The ability to exert self-control is typically called willpower. Willpower is what allows people to direct their attention, and it underlies all kinds of achievement, from school to the workplace. There is significant debate in science as to whether or not willpower is a finite resource. Some well-known studies have made a case that exercising willpower makes demands on mental energy.
This concept, called ego depletion, is one possible explanation for why individuals are more apt to reach for a chocolate chip cookie when they're feeling overworked.
Recently, however, scientists have failed to replicate some of the studies underlying the concept of ego depletion. More research is underway, but the final verdict on whether people can “run out” of willpower remains to be seen.