Strong and diverse press, strong and enforced rules against politically motivated decisions. A judge should know that, if they don’t strictly follow the law, they’ll lose their job. This won’t make the thing perfect, but far better than officially political judges.
By competition and diploma. A judge is a legal technician. Why elect him on political bases? We do not elect an engineer on political criteria, we take the one who seems the best among the candidates.
What’s the point to elect them?