The law is generally based on what people feel is moral. However because of democracy it is a more universal take on morality.
Respectfully, you missed my point (or I did a poor job explaining), but the point was, it's not the law that makes something right or wrong, the law is an expression of a human value system. Values--->Morality--->Right/laws, in this order.
The question then would be were do those values come from.
Where do systems of measurement come from? We create them. Why? because they are useful. And while measurement isn't a perfect analogy to morality, it is in some ways. It is a system that humans created in order to solve a problem and attempt to realize certain outcomes.
For example, just as every person could benefit from not being moral and instead did whatever they wanted, with respect to measurement, every person would similarly benefit to claim that the 20 gallons of gas they just pumped was not a system they recognized and instead wanted to pay for 1 gallon. So why don't people claim that 20 gallons is really 1 gallon? Because they know society has settled on what a gallon is and endorse a system of enforcement for people who cheat.
So why do people agree on systems of measurement when every person would be better off ignoring the rules? Because enough people have been convinced to learn and support the enforcement of that system to the benefit of society and in turn the people in it.
Like morality, it is simply a social convention that is useful. We created the system of measurement subjectively, meaning that the length in space that we call 1 foot wasn't chosen arbitrarily, it was chosen because it is useful for measuring certain things, but 1 foot isn't something that exists objectively, out there, it is subjective.
This is also true of games. Take baseball, the system was created and agreed upon. Every player would benefit from cheating and yet the overwhelming majority of players follow the rules, despite the fact that cheating can be worth 10's or even hundreds of millions of dollars.
And what happens to players that cheat? When caught, they are punished or banished from the group. It's a made up system and yet it works, and all for a silly game. Imagine what humans could do if the lives and well-being of others was on the line! But, of course, we don't have to imagine, those systems exist!
The question then would be were do those values come from.
Simple, as a result of human experience, processed though reason, wisdom, intelligence and empathy.
Of course we all experience life a little differently, but when it comes to the worst things, pain, suffering, sickness, societal chaos, insecurity, we all very much wish to avoid that which is why the worst things are often universally valued by people of virtually all cultures. We can say that we value avoiding these states wherever possible....This is where we get ideas of freedom and liberty. These ideas say that we can experience life as we want as long as it doesn't harm others or yourself. Now that was a bit overly simplistic, so if you want to break that down more let me know.
Experience--->values--->morals--->rights/laws.
Culture could be said to be the amalgamation or the expression of all of these things together. In retrospect, your answer wasn't wrong, just imprecise.
All created by humans to attempt to realize a particular state of affairs.
Why are things still messed up?
Human greed, inability or unwillingness to enforce rules/ laws, a lack of wisdom, knowledge and understanding that thankfully has become better over time and has consistently lead to a better world when taken as a whole.
-Cheers