It is a concretely-established psychological 'fact' that we tend to commit the self-serving bias: when we do something good, we tend to believe that it is dispositional - it is our personality, character and skill that resulted in the good we did, and if we do something bad, we tend to attribute it to the situation - we were unlucky, or the situation was simply not in our favour.
Research has generally shown that this bias exists because of three main reasons. It could be motivational, as people create causal justifications that preserve their self-esteem and make themselves feel better. It could be a form of impression management, as engaging in self-serving biasness leads others to think more favourably of us. Yet another reason looks at the cognitive - the basic mechanism of memory may be geared such that people recall how their dispositions lead to success and how their situational environments lead to failure more easily than vice versa.
In exercising my penchant for evolutionary and adaptive thinking, none of these explanations are sufficiently satisfactory, as they merely brush the surface about how the psychology plays out but do not consider how and why it has come to exist. Why on earth do we indulge in the simple act of self-praise, with the reverse implication of committing the fundamental attribution error on others when they fail (i.e. we think they are dispositionally to blame for their failures)?
In brief, the idea simply is that the self-serving bias allows those who engage in it to have access to future opportunities at proving their worth. By attributing your successes to yourself instead of, say, luck, others are more likely to count on you in future because they are more willing to rely on you to do what you just successfully did. You will then be less likely challenged, as competitors will be discouraged to take you on. This increases your power and leverage over that particular task which incrementally adds to your worth on the whole, which has always been important to survival regardless of whether we're hunter-gatherers or people living in a modern world. As we all (should) know, power is the means to access to mates which is the means to gene-propagation. The self-serving bias in times of failure also proves to be a very useful tool to mitigate loss of power. In attributing your failure to being unlucky - the circumstances were simply not good, your professor disliked you and denied you your A - you will dampen the perception of your incompetence, thereby increasing your chance at opportunities to prove your worth again in future.
The clincher is this - this hardly has to play out consciously at all. The self-serving bias appears not to be a learned tendency which, in my opinion, discredits the motivational and impression management explanations as causal reasons for its existence. The fact that very young children start engaging in the self-serving bias the moment they can make coherent decisions and justifications shows that we could very well be instinctively wired with this survival tool at our disposal and the more unconscious we are of our usage of this tool, the better we are at 'deceiving' ourselves and convincing others to give us yet another shot. This points to an innate design of human nature that has evolved over a long time, such that those endowed with the tendency to use the self-serving bias prevail over those who don't or are simply not equipped.
Admittedly, these thoughts spun through my head while I was finally getting my lazy ass out to run. Even though the distance was solid, I stopped and walked three times which is really poor running form compared to myself in the past, and - there you guessed it - I was drowning in self-serving thoughts as to why I couldn't sustain my pacing. A little self-awareness goes a long way.