They're "supposed to" be appointed. This is the way it used to be and the way it constitutionally has to be at the federal level. But I think more than half of states have elected judges.
True, they often run unopposed, but that doesn't free then from political pressure; if they make an unpopular decision, any lawyer could opt to run against them by simply campaigning as not the other judge. They would usually win and get a fantastic résumé boost.
It varies from state to state (perhaps even county to county) In some places they are appointed, in others they are elected.
When they are elected, even if they run unopposed they still tend to act tough on crime. If a judge or prosecutor started acting weak, the could very quickly find themselves opposed in the next election.