In Gideon v. Wainwright, 372 U.S. 335 (1963) the United States Supreme Court ruled that the Sixth Amendment grants an indigent defendant the right to state-appointed counsel in a criminal case that could result in incarceration. The same rule applies in criminal contempt proceedings other than summary proceedings under United States v. Dixon, 509 U.S. 688 (1993).
As to appeals, the rules are more complicated. First, there is no federal constitutional rule that appeals be allowed in state courts at all. If appeals are allowed, though, criminal defendants threatened with or subjected to incarceration have a right to state-appointed appellate counsel on their first appeal of right.
My last blog post was an analysis of a June case of the Supreme Court of the United States as to a civil contemnor's right to appointed counsel. Later on, I'll discuss the law of appointed counsel in civil matters.