I think the others in this thread has already made some really good points, though I'd like to add that probably one of the biggest pitfalls of not only horror,
but any serious story in general, is to have an otherwise dark and mature atmosphere, but then tacking on silly jokes geared towards kids as an afterthought.
Comedy in horror can work very well if the comedy itself is related to the story and setting, and will often work best with dark humor or gallows humor,
whereas the works that fail usually have goofy jokes that feels like they were lifted straight from a kid's show and doesn't fit with the tone or atmosphere at all.
One of the worst offenders in this regard are the Gargoyles in Disney's The Hunchback of Notre Dame (I know it isn't a horror movie, but still a good example of how bad comedy can ruin good storytelling),
for while they serve as fun sidekicks for the protagonist Quasimodo to discuss his motives and intentions with in the beginning of the movie, they very quickly wear out their welcome when the story takes a serious turn,
and at several times the rest of the plot is forced to a grinding halt so that the gargoyles can do some dumb slapstick or pop-culture references.
Probably the low point of the film is when we've just seen the villain tearing up Paris, burning houses with people still locked inside and committing ethnic cleansing
of the romani minority, only for the movie to cut to the Gargoyles joking about grilling a sausage above the flames and having a song number full of pop-culture gags.
In my opinion, if you want to use comedy in horror, you should try to:
- Use dark humor to highlight the grim situation
- Have the characters make a quip about how absurd it all is, being hunted by monsters/stuck in a haunted house/whatever the situation is
- Having somebody be killed in an ironic fashion
- Having the characters joke about each others fears and flaws
But you should avoid:
- Using goofy kid humor, like jokes about polka-dotted underwear, potty humor, silly dancing excetera
- Making pop cultural references to works that aren't horror
- Making jokes during the tense climax and third-act resolution of the main plot
- Adding a comedy relief character who has no story purpose or personality outside being "funny" (this is how you end up with Jar Jar Binks)