I'll take a crack at it. The decision is
here.
This is my personal opinion and should not be taken as legal advice. If you need such advice, please contact a licensed attorney in your jurisdiction.
In 2005, Cosby was accused of sexual assault against Andrea Constand. The District Attorney (Castor) is alleged to have looked into this, and concluded that there was insufficient evidence to convince a jury that Cosby was guilty beyond reasonable doubt. Castor then used his discretion as DA to decline prosecution. By doing this, Castor prevented Cosby from being able to use the fifth amendment's protection against self-incrimination in subsequent civil actions. By his own admission, Castor intended to remove "for all time" the possibility of prosecution. Deprived of this ability, Cosby provided four sworn depositions that contained incriminating statements. The incriminating information can be found starting on page 14 of the decision. Cosby later settled with Constand in the amount of 3.38 million dollars. In 2015, the new district attorney, Risa Vetri Ferman, decided to prosecute Cosby. Ferman asserted that in the absence of any written agreement not to prosecute, the state was not bound. The trial court agreed. Additionally, the information he provided at these depositions was permitted into trial as evidence. Cosby was convicted.
The court reversed this conviction. I can sum it up best with a quote from page 52: "when a prosecutor makes an unconditional promise of non-prosecution, and when the defendant relies upon that guarantee to the detriment of his constitutional right not to testify, the principle of fundamental fairness that undergirds due process of law in our criminal justice system demands that the promise be enforced"
Cosby was told he wouldn't be prosecuted by the entity with the authority to make that promise. Relying on that promise, he made statements under oath that he reasonably believed he had to make (no fifth amendment right, see page 66). The new DA cannot simply come along and decide to break the agreement. Note that this does not address the wisdom of the former DA's decision. However, just because a prosecutor makes a very bad decision does not entitle the government to a remedy at the expense of the defendant's rights. Allowing Cosby to be prosecuted, however, would allow prosecutors to do an end run around the fifth amendment. The court considered the remedy of allowing a third trial where the testimony from the depositions would be disallowed. This was rejected, on the principle that when there is a due process violation (which they found here), the only fair remedy is putting the offended party as they were before the offense. The court reasoned that the actions of the first DA were bad enough that no retrial could be allowed. You can see reasoning for this starting on page 76. The reasoning staring at the bottom of page 77 and going on to "IV. Conclusion" on page 78 is too lengthy to quote, but it sums up the case nicely.
tl:dr version: The former DA made an agreement not to prosecute Cosby. The fact that a new DA decided differently does not allow them to reverse the decision.
Note there is also a question as to if several of the women who testified against him (past alleged victims) should have been allowed to do so. The court declined to answer this question because the case was moot. I will therefore not go into any analysis of this issue, other than to say that I agree with the notion that courts should not address more issues than necessary to resolve a case.
EDIT: I just want to add that people upset at this aren't wrong. It's bad things turned out this way. Had the court ruled differently, it would have potentially, as I said above, allowed prosecutors to do end runs around the fifth amendment, to people far more sympathetic than Mr. Cosby here. That is one of the parts of living in a legal system where precedent is extremely powerful.
We can take steps to prevent things like this from happening again, such as actions to restrain the power DAs have to make agreements like this. Should Mr. Cosby be in prison? Morally, yes. Legally? Not unless they have something else they can nail him on.