window.location.hash includes the hash symbol.
if (window.location.hash == "#red"){
$("body").addClass("red");
} else if (window.location.hash == "#green") {
$("body").addClass("green");
}
Additionally, your inline-style that you set to make the body yellow will override anything you do with a class (unless you use !important, but don't do that!), so you'll want to make it yellow in the stylesheet rather than inline.
You'll note however that once you make it red, then green, it stays green. This is because you never actually remove the classes, so it takes on the one that has the highest specificity (green in this case since it is last in the stylesheet.) To remedy this, you'll want to also remove the other class.