Generally speaking if it is substantively unconstitutional for the government to punish a certain kind of criminal conduct, then this provides a basis for a "collateral attack" on the conviction to set it aside (in a post-conviction motion in state or federal court), even if the criminal defendant serving a sentence has already appealed the conviction directly (i.e. to the court of appeals and all higher appellate court that review that court of appeals) to the fullest extent possible and lost.
But this is not self-executing. Someone has to file a court case on behalf of the person incarcerated to make it happen. It does not generally entitle the criminal defendant to compensation for wrongful incarceration or to a return of fines paid, although it would end the authority of the state to incarcerate the individual going forward or the compel that individual to pay the balance of unpaid fines.
There might be a right to have the case sealed, but the ruling does not automatically remove the conviction from the criminal record of the defendant. And, getting the case sealed is likely to be harder in the circumstance where all of the sentence for the unconstitutional offense has been fully served.
When a past conviction does not meet the standards of current law from a procedural context, in contrast, this may be raised in pending direct appeals, but is rarely something that can be brought up in a post-decision collateral attack on the conviction unless certain other exceptions apply (e.g. sometimes if a collateral attack pending before the decision is still pending at the time of the decision or in certain extraordinary cases involving due process concerns that cast deep doubt on the validity of all convictions obtained by that means), and even in cases where it is set aside, does not pose a double jeopardy bar against a new prosecution for the crime that meets the appropriate procedural standards.
The U.S. Supreme Court is currently considering the proper treatment under these rules of its holding that non-unanimous juries were an unconstitutional manner by which to convict someone of a felony, which is a procedural rule, but may fall within the exception that makes the new rule retroactive anyway.
For cases on direct review, a new rule for the conduct of criminal
prosecutions is to be applied retroactively to all cases, state or
federal, pending on direct review or not yet final, with no exception
for cases in which the new rule constitutes a ‘clear break’ with the
past Justice Harlan's habeas approach was first adopted by a plurality
in Teague v. Lane and then by the Court in Penry v. Lynaugh. Thus,
for collateral review in federal courts of state court criminal
convictions, the general rule is that new rules of constitutional
interpretation—those not 'dictated by precedent existing at the time
the defendant's conviction became final'—will not be applied. However,
[a] new rule applies retroactively in a collateral proceeding only if
(1) the rule is substantive or (2) the rule is a 'watershed rul[e] of
criminal procedure' implicating the fundamental fairness and accuracy
of the criminal proceeding. Put another way, a new rule will be
applied in a collateral proceeding only if it places certain kinds of
conduct beyond the power of the criminal law-making authority to
prescribe or constitutes a new procedure[ ] without which the
likelihood of an accurate conviction is seriously diminished. In
Montgomery v. Louisiana, the Court extended the holding of Teague beyond the context of federal habeas review, such that when a new
substantive rule of constitutional law controls the outcome of a case,
state collateral review courts must give retroactive effect to that
rule in the same manner as federal courts engaging in habeas review.
As a result, at least with regard to the first exception, the Court has held that the Teague rule is constitutionally based, as
substantive rules set forth categorical guarantees that place certain
laws and punishments beyond a state’s power, making the resulting
conviction or sentence . . . by definition . . . unlawful. In
contrast, procedural rules are those that are aimed at enhancing the
accuracy of a conviction or sentence by regulating the manner of
determining the defendant’s guilt. As a consequence, with respect to a
defendant who did not receive the benefit of a new procedural rule,
the possibility exists that the underlying conviction or sentence may
still be accurate and the defendant’s continued confinement may still
be lawful under the Constitution. In this vein, the Court has
described a substantive rule as one that alters the range of conduct
that the law punishes, or that prohibits a certain category of
punishment for a class of defendants because of their status or
offense. Under the second exception it is not enough under Teague to
say that a new rule is aimed at improving the accuracy of a trial.
More is required. A rule that qualifies under this exception must not
only improve accuracy, but also alter our understanding of the bedrock
procedural elements essential to the fairness of a proceeding.
From here.