All federal judges are appointed to lifetime terms, as per the constitution. The phrasing is a bit weird, so I won't quote it.
States may, like anything else, set their own rules. There is no (federal) constitutional requirement that non-federal judges be appointed or elected.
There is thus no rule that can be identified. Balllotpedia has a nice overview. Most states use the same method for all judges, but there are exceptions, e.g. South Dakota and Tennessee. The closest to a rule that I can see is that if a state does use two or more methods, they tend to appoint the higher level judges (e.g. the state Supreme Court) and elect lower level judges (e.g. trial judges). But even that has at least one exception, Montana, where the Worker's Compensation court is appointed but the state Supreme Court is elected.
Some states also have retention elections. In a retention election, a judge runs unopposed and people vote yes to retain or no to send to a regular election. Retention elections usually are non-controversial and retain the judges. Retention elections may appear in states that appoint, like Iowa, or in states with elections, like Pennsylvania.
Other states may have limited terms where the judges can run for reelection against opposition. Or be reappointed.