First of all, it is important to think of "law" as an alternative to war and violence. Three thousand years ago, no one had rights. Powerful parties were free to pillage, rob and slaughter. You couldn't take these people to court.
A tooth for a tooth
In ancient Babylon, the ethic of "an eye for an eye, a tooth for a tooth" evolved. It appears in both the code of Hammurabi and in the Torah. It evolved not to protect the little guy, but as a way for competing factions to avoid bloodshed. If a henchman of one band lost an eye, he was able to literally "sue" for the plucking of his attacker's eye. Eye-for-an-eye established proportionality as a principle: one eye for one eye, and no more.
Gradually, a legal profession formed to represent these factions. In Greece and Rome, lawyers represented parties who were wronged or harmed, seeking satisfaction. Lawyers were seen as friends of the plaintiff who knew the right things to say in court. By the first century AD, lawyers were seen as an actual profession.