There are really two things going on here. The first is that the winner writes the history so they appear to be the good guy. The second is that being the aggressor, who in hindsight is usually seen as the bad guy, is much harder than being the defender. To truly win the aggressor has to not only have military superiority but be able to either win the hearts and minds of the majority of the people they just invaded (obviously incredibly difficult) or kill a huge majority of the native population so that they can remove their occupation while still calling it their territory. Also, even when aggressors do win, they usually aren't content with what they have and go for more until they eventually lose which is recorded as a loss as a whole. All the defender has to do is not surrender until the aggressor just decides to leave. If they do that the only way for the aggressor to win is to kill the entire defense, which if their goal is to rule the defense, would be pointless.