From the days of newspaper TV Guides that would write two sentence synopses (loglines) of a television show. Sometimes these summaries wouldn't sound comedic on the surface, so "hilarity ensues" was used to ensure the audience understood it was a premise for a comedy.
Tonight on Seinfeld, George decides the best time to have a snack is during sex. Hilarity ensues.
An ending that can be put onto the ends of stories and plans. A convenient phrase to use when your story or plan sounded really dumb. It usually doesn't make much sense.
Me: Ok here's what's gonna happen: I'll go steal her key's and she will spend the whole night trying to find them!
Other People: (stare)
Me: Hilarity ensues.
Something funny is upcoming due to a build-up of circumstantial events, or a comedy of errors.
Sometimes involves rape.
You: Fuck! I just shat my pants!
Rape Panda: *Waits for hilarity to ensue*