First, the proper immediate action is to - while not leaving the judicial branch - appeal up to the next higher court. In federal courts, the lowest levels are district courts, which issues decisions appealable to the circuit courts, whose decisions are sometimes able to make their way to the Supreme Court by submitting a Petition for Writ of Certiorari. Then the Supreme Court meets and decides which appeals it will hear for the year ahead.
This works similarly on a state level, except the levels of the courts may have different names (for example, Washington, DC’s trial level court is not the District Court, but the Superior Court. In Virginia, one trial court is the General District Court, which hears claims up to $25,000 and another trial court is the Virginia Circuit Court which hears other claims, and some felonies while also serving as an appellate court for the General District Court).
Assuming those options have been exhausted, the reality is yeah, sometimes there are judges who will act that way. Sometimes they will get away with it because they are maintaining enough discretion, they are powerful enough, or because someone else is corrupt, too.
A number of options and variables may come into play at this point. What, if any, laws exist to prevent the behavior in question? What ethics standard does the jurisdiction have (this would likely be developed by the local bar association)? Are there any other organizations he or she is affiliated with professionally? Who else knows about it, how serious is it, will anyone else speak up, etc...? Each of those variables - and likely a number of others not mentioned here - may play into the outcome. They each can serve to make one’s actions more or less easy or desirable to perform, whether by exposing and embarrassing an actor, reminding him or her of the potential legal liability associated with such actions, etc.
All that said, there are limits on what a judge may or may not do. For example, in federal criminal cases, the Federal Sentencing Guidelines, rules assembled by the US Sentencing Commission that are, of course, non-binding, set a floor and ceiling on the years in prison one should be sentenced to in various circumstances with a formula used to make the calculation. It is rare (and sometimes controversial and subject to scrutiny) if a judge grossly or unreasonable deviates from the Guidelines. The intent of the Guidelines is to remove judicial bias from the doling out of prison sentences. Results have arguably been mixed.
Finally, I’d be remiss not to mention that in general judicial discretion in many aspects of litigation is a desirable and useful occurrence, whether it manifests itself in the dismissal of a frivolous case or appointing counsel to an indigent defendant.