According to Freud, the ego is the part of personality that helps us deal with reality by mediating between the demands of the id, superego, and the environment. The ego prevents us from acting on every urge we have (produced by the id) and being so morally driven that we can't function properly.
The ego works according to the Reality Principle which helps us direct our unacceptable sexual and aggressive urges to more acceptable targets. For example, when you walk down the street and see an extremely attractive person, the ego, working on the reality principle, helps us realize that it is not socially acceptable to cross the street, grab the person, and have sex with them. Instead, the ego tells us that there will be other, more appropriate people, places, and times to fulfill these needs.