Laws, law enforcement agencies, and courts are nearly universal features of government.
Every society is faced to some extent with the problem of people who violate the rights to life, liberty and property of others and who act so as to undermine order or public morality. If such threats are left unchecked, society will be weakened or even undermined. Thus any society needs some way of mitigating or eliminating these threats.
Crimes must be punished, whether with fines, imprisonment, or even execution. A basic principle of justice is that punishment should be proportional to the crime. When possible, criminals should be made to make restitution to their victims. Imprisonment prevents crimes from occurring by keeping criminals away from the public. Any punishment deters crimes from occurring by providing a disincentive to crime.
It appears that only government can provide law enforcement. This is because law enforcement is inherently coercive, while the free market depends on the absence of force. It appears inevitable that some entity will achieve a near monopoly on force in a given territory. It seems that the best that can be achieved is to limit its powers and try to make it serve the public as much as possible.
The power to coerce and imprison is dangerous, particularly in the hands of the government. Allowing the government to use punish whomever it wants is a recipe for tyranny. Thus there is a need for law. That is, there should be a set of rules that determine which behaviors will be punished and which will not. This will minimize lawbreaking, since people will better know what actions to avoid.
But government must also follow the law. Laws that criminalize actions after the fact, called ex post facto laws, can be used to punish individuals rather than protect society. Laws should be as clear as possible, so that people do not innocently fall afoul of them and they cannot be used to harass innocent people. Laws should apply to all people, not only some. In particular, laws should apply to the government, as well as its subjects. This is called the rule of law.
The rule of law promotes economic prosperity. Government takes money from people through taxation, and private criminals take money from people though theft. But if theft is systematized, then at least people can plan their actions. This greatly reduces risk, and allows greater economic growth. Thus government theft can be better than private theft, even when the government takes more. Even governments that don't care about their subjects have an incentive to promote the rule of law, since then there will be more production for them to tax.
Self-defense can prevent and deter many crimes. But it is not applicable in many cases. Given certain levels of population and crime, police are necessary to enforce laws. However, their powers must be constrained if they are to serve the public. In many foreign countries, police corruption is endemic. Even in America, police in New Orleans participated in illegal seizures of guns in the wake of hurricane Katrina.
If the government wants to punish someone, a court is necessary to determine innocence or guilt. The writ of habeas corpus requires that someone who has been arrested appear before a court. This stops the government from holding someone for an arbitrary length of time without charges. Civil liberties such as protection against unreasonable search and seizure, jury trials, presumption of innocence, protection against self-incrimination, protection against double jeopardy, and more help to protect the innocent from being falsely convicted.
The attitude that if people have nothing to hide, they should not worry about civil liberties reverses the proper attitude. It is the government that seeks the punish people. If the government has nothing to hide, it should not fear protecting civil liberties.
People should never assume that just because someone has been charged with a crime, "he must be guilty of something". There have been far too many cases of people charged with crimes who have been completely innocent. There are even cases in which the prosecutor knew this to be true.
Some judges seek to enforce their own conception of justice by bending or ignoring the law. This can occur in both criminal trials and "judicial review" of laws. But judges have no special competence in lawmaking. There is no reason to believe that their decisions are better than those of legislators. Even if a judge is right in a particular case, ignoring the law does great damage to the rule of law. Thus judicial activism can have very negative consequences.
Laws, police, and courts are necessary to protect society. But they can be dangerous as well. Civil liberties and the rule of law must be protected to minimize the danger.