So if your country/state is full of nazis who want to go on a genocidal rampage, and use democracy and law to that end, then they should just be allowed to do it?
This is actually irrelevant.
The Nazis actually
did attain power through democracy and law and despite the fact that the Weimar republic had a functional judiciary branch, judges did nothing to stop Hitler because they couldn't do anything.
Judges have no coercive power on their own. The judiciary system works because the two other branches actually listen to it. It cannot actually force change but must rely on the other components to enforce and give effect to its decisions. The government and legislative assembly is game to play, so they play. But when they decide the game is over there isn't a single thing the judiciary can do to stop them. It could theoretically refuse to enforce new laws or to recognize changes, unlawful or not, in the existing body of laws, but the government will just sack the recalcitrant judges (or worse) and find lapdog judges who will be all too happy to enforce the new order.
When von Hindenburg passed the Reichstag Fire Decree on behalf of his great pal Hitler the judges couldn't stop him. When the Reichstag and the Reichsrat passed the Enabling Act for their great buddy Adolf the judges couldn't do anything. If a Nazi party gets elected here and passes version 2 of these two things, judges won't be able to stop them.
Law exists only insofar as the central government is willing to employ coercion to enforce it. If not, legislation and jurisprudence and all that good stuff are just words on paper. The judiciary is actually the weakest branch out of the three because its power is not innate but conditional on the support of the legislative and executive and their institutions.
There is such a thing as tyranny by majority, and this is something that must be prevented. Such an ideological approach to law, either as a lawyer or a politician (and the two aren't at all mutually exclusive), defeats the entire damn purpose: to uphold society through justice and equality.
Is that the purpose of law?
Is the Uganda Anti-Homosexuality Act of 2014's purpose to uphold society through justice and equality? If it isn't, then is it still a law or not?
Law is not about justice and equality, at least not when considered in a vacuum. You are mistaking the content for the container. What law is is simply a set of norms that we, as a collective, have chosen to govern ourselves and submit ourselves to. Its purpose is ordering society without any presupposition as to the way it actually does this. Nothing more, nothing less.
Our judicial system places great emphasis on equality and fairness because we, as a people, have chosen to be governed in such a way. It is the modern social contract. There are many other systems who don't. Does that remove their status? Are they not legal systems because another society has different values and standards and desires to be governed another way?
In Shari'a law, non-Muslims cannot testify against Muslims so the latter can do pretty much what they want to the former as long as there are no other witnesses or they are other dhimmis. So even such a basic component of law as conflict resolution between two private parties isn't necessarily dealt with in a fair way everywhere. It offends us to read this, but in countries governed by shari'a it is pretty much the norm because non-Muslims are viewed as inherently untrustworthy. So as you can see law's purpose is not necessarily to be "fair" or "equal". This is a Western conception of law and it is relatively recent in execution at that.