This is a fascinating question to consider. The concept of justice in the play is a major theme and is worthy of some serious attention. This play has been called the most brutal of all of Shakespeare's tragedies, and we can understand why when we consider the way in which one terrible and catastrophic event follows yet another. This of course causes both us and the characters to ask whether there is any such concept as "justice" or whether we live in a world that is at best indifferent to us, if not downright hostile. Gloucester believes the latter, painting a picture of capricioius gods who "kill us for their sport." He thus questions whether we are able to expect the natural world to understand and demonstrate the same concept of justice that is present in the human world. Edgar, by contrast, believes stubbornly that "the gods are just," arguing that the fate of individuals is decided in accordance with their actions and faults.
However, the ending of the play, and in particular the soul-destroying image of the grief-stricken Lear cradling the broken body of Cordelia in his arms, challenges such hopeful notions of justice, as both the good and the wicked are shown to die. Loyalty or any form of goodness seems to have no bearing on your eventual fate, and in spite of Edgar's loyalty and his belief in justice, the injustice that pervades the play and the lack of a happy ending seems to mock the concept of any divine order or system of justice. Loyalty or lack of loyalty has no bearing on whether or not you receive a happy ending.