It seems to me, most simply, the 'law' is a set of rules that are enacted to govern conduct.
I suppose that depends upon which 'law-making' authority we choose to direct our focus. In the United States of America, federal laws are passed and enacted by the U.S. Congress. However, the question of how laws come to be is a bit more involved than I would think we have time and space for here. Some laws begin as mere ideas that spring from the need to maintain order, while others are attempts of particular interests to unduly and unfairly press their weight on others, such as was the case with laws during the Jim-Crow era southern United States.
However, here is an audio-visual that might explain how, in particular, many laws come to be in the United States:
[youtube]mEJL2Uuv-oQ[/youtube]
YouTube - ‪Schoolhouse Rock- How a Bill Becomes a Law‬‎
How does it do what it does?
This is an awfully broad question. I am assuming you mean that if a law is designed to maintain order, how does it do that. So, my first inclination is to say that with most law codes, there must be an instrument of enforcement, such as our Executive Branch of government here in the United States. Sorry, but while I'm certainly no expert, our Constitution in the U.S. is the model I'm most familiar with when it comes to some of these questions. The Executive Branch is the limb of government that is tasked with enforcing the laws that Congress passes. While the Judicial Branch is tasked with interpreting those laws. Urr, that is how it is to work ideally. We often really screw that up here in the states.
What is the relationship between "law" and "rights"?
I am not sure there is an inherent relationship between these two items if we view them dispassionately as mere concepts and models. However, again, going back to the United States Constitution and many state law codes here in the U.S., laws are often enacted as an instrument for 'protecting rights'. We hold certain truths to be self-evident, that all men and women are born with certain unalienable rights, and our laws are measures we enact to try to protect those rights, although I have yet to witness a perfectly functional model when it comes to codes of law, whether that be civil or criminal.
Of course, after all this, I just realized perhaps you were talking more specifically about 'religious laws' since this is the religious section. In that case, please ignore all of the above and accept my humblest apologies for intruding.