As I understand it, the laws of war (Geneva convention, etc.) apply equally to soldiers no matter which side is responsible for the war. So an invading Nazi soldier had the same right to kill a Polish soldier who was defending his home and family, as the Polish soldier had to kill the invader. Even if the invader’s victory over the defender enables the invader to commit atrocities, while the defender’s victory prevents that, under the laws of war both soldiers are combatants with the same rights to kill each other, and same restrictions on how they can go about it.
If someone breaks in to my house, intending to harm me or my family, I have the right to resist them with force, and my resistance does not give them the right to kill me. Yet if an invading army “breaks in” to a country, people who resist them with force are combatants and can be legally killed by the invaders.
How did this come about?
The book for this is Michael Walzer's Just and Unjust Wars, an excellent survey of the development of the Law of Armed Conflict and its implications today.
In summary, you're asking about the difference between jus ad bellum (the law of going to war) and jus in bello (the law within war). Jus in bello generally recognises four principles:
Military necessity: Attacks must only be made against military objectives that are intended to defeat the enemy.
Distinction: Combatants must be distinguished from civilians. Combatants may be attacked; civilians may not.
Proportionality: The force used and the damage inflicted on targets must not be excessive in relation to the military advantage expected to be gained.
Humanity: Unnecessary suffering is to be avoided, and non-combatants such as civilians, soldiers who have surrendered, and the injured, should be treated correctly.
To punish soldiers of an aggressive power for the actions of their government would be to effectively violate the principles of necessity and proportionality. Little would be gained from it, and it may even be detrimental to the defending power's war effort: soldiers are not assumed to to be responsible for their state's decision to go to war, and are assumed to have moral equality with those they fight. That is, it is understood that that soldiers have the right to both defend themselves and to try to kill the enemy, regardless of the legality or morality of their cause. Unlike the burglar in your example, their right to this is defined by their being lawful combatants, which is defined by their wearing uniforms and distinctive national emblems, and operating as part of a legitimate chain of command. Those who do not wear uniforms but do fight are considered to be unlawful combatants, and may be prosecuted under civilian criminal law.
It would simply not be practical to hold every single soldier of an aggressive state responsible for his government's actions. Indeed, it may even be detrimental: if a soldier knows that he is already a war criminal from the moment he crosses that border, what incentive does he have to obey the other laws and customs of war? If he is caught in a hopeless situation, would it not be better to fight to the death rather than surrender to face certain punishment?
At risk of breaking the twenty-year rule, a real-life example of this occurred in 2004 when Jeremy Hinzman, a U.S. Army soldier, deserted to Canada in protest of the Iraq War and applied for refugee status, claiming that he could not accept personal responsibility for fighting in what he regarded as an illegal war. His claim was rejected, and on appeal, the Federal Court of Canada upheld the decision, with the judge writing that:
An individual must be involved at the policy-making level to be culpable for a crime against peace... the ordinary foot soldier is not expected to make his or her own personal assessment as to the legality of a conflict. Similarly, such an individual cannot be held criminally responsible for fighting in support of an illegal war, assuming that his or her personal war-time conduct is otherwise proper.
This is not really a historical question, but a practical one. The reason why the laws applying to the conduct of war, international humanitarian law (IHL) are applied equally to the aggressor and victim is summarized by jurist Hersch Lauterpaucht: "it is impossible to visualize the conduct of hostilities in which one side would be bound by rules of warfare without benefiting from them and the other side would benefit from them without being bound by them". In other words, if it were otherwise, IHL wouldn't function at all in its role of protecting civilians and prisoners of war.
However, there is a dispute as to whether IHL has enabling effect, allowing legal killing in certain circumstances, or whether it simply forbids certain actions. Adil Ahmad Haque is of the latter view (see his book Law and Morality at War). According to the latter view, soldiers who participate in illegal wars of aggression may not be violating IHL, but their actions are not seen as justified.
The legal equality of IHL (known as belligerent equality) is separate from the moral/ethical question of whether soldiers on both sides are equally justified in killing the enemy. This hotly debated issue is known as moral equality of combatants (MEC) and has been described by jurist Eliav Lieblich as "one of the stickiest problems in the ethics of war". Walzer is a supporter of MEC, arguing that political and military leaders bear the entire responsibility for an unjust war. Others disagree, an aspect that /u/Rob-With-One-B doesn't address in his answer. For example, courts in other countries have ruled differently on the issue of whether soldiers should be allowed to refuse to fight an illegal war. A great book looking at legal implications of the revisionist view rejecting MEC is Tom Dannenbaum, The Crime of Aggression, Humanity, and the Soldier.
Walzer's book is certainly influential but its focus is more on philosophy/ethics, so I wouldn't recommend it if what you're looking for is a legal history. For a well-received history of the making of modern IHL, I recommend the new book by Boyd van Dijk: Preparing for War: The Making of the Geneva Conventions.