Shame is a more sophisticated emotion, as it needs a notion of the Self and the capacity of building up mental representations. It comes up by the age of two. Firstly, kids need to recognize themselves (this is myself), before feeling ashamed. At the same time, there is also an internalization of family and social rules and values, that allows kids to evaluate its own behavior. With these features on their mind, children are able to decide if they are responsible for what happened and if their actions are bad or right.
Being more specific, shame involves a global evualtion of the Self as being bad, weak or fail, comparing to social group values or even to the goals that we create for ourselves. We also feel shame when we are humiliated and bullied. The function of shame is to prevent unexpecting responses from others, keeping us safe and secure. It informs us that sometimes it is important to be cautious in our social relations so we are less likely to be criticized or threated. When we feel shame there is a strong need to connect to others and be validated.
While shame is activated when we have a global evaluation of the Self as a failure , guilt has to do with specific atittudes or behaviors that lead to bad consequences. The function of guilt is to prevent the group to be fragmented, keeping up with cooperative behaviors. Guilt can be an important form of dominating others, being used by religion and politics with this goal.