Much like science fiction and fantasy, humor and horror are close cousins. Both genres rely on a suspenseful build to a surprise. Both are entirely subjective and both are difficult to do well.
The setup
Suspenseful expectation is critical for humor and horror writing. A monster needs to be made terrifying over time or it won’t scare anyone. The same rule goes for a comedic situation that gets funnier as it develops.
Think of the similar build that runs through “Curb Your Enthusiasm” and “the Call of Cthulhu.” Both sources use shocking details and (sometimes) grotesque descriptions to elicit reactions. In each, there is a balancing element, such as a straight man or a character who serves as the voice of reason. Just as importantly, there is an element of the unknown in which the point of view character faces the unexpected.
Timing and execution
Not everyone is funny. That’s fine, because not everybody is scary either. However, the delivery of the surprise is a critical skill for both genres. The difference is often what the surprise is and how it is presented. An overly simple example is having a character walk in a dark hallway when she slips and falls. If she slips on the blood of a severed head, the story is horror. If she slips on a banana peel, it’s humor.
Subjectivity
The difference between humor and horror is the difference between a Dracula movie starring Gary Oldman or one staring Leslie Nielsen. Even if you use the same script and cast other than the lead, the outcome will differ because the subjective expectation changes the delivery.
An intertwined connection
There are noises in your kitchen at 3 a.m. If you walk downstairs to investigate and find that the noises came from your cat’s insatiable lust for eating and vomiting parsley, then you’ve felt the tense buildup and release of humor. If, after cleaning up cat puke, you turn around and see the broken lock on your now open front door, then you know how close humor and horror are.