I always love the O Henry endings but trying to write them is a bit tricky. That's because they're tricks. This is not something for the novice writer to try. Appealing, yes but hard to pull off. I've done it once and that took a lot of planning and weaving through the story. That's the trick. The seeds must be planted but with a subtle hand. Otherwise the ending of this type falls flat
How does this work. What happens is the story comes to an end but there isn't an ending that satisfies the progress of the story. What the writer needs is an escape hatch and that means blending in things that were only hinted at causing the reader to laugh and say of course.
A trick ending can take a story that could have ended like a hundred other stories and turn the story into something interesting. But to do this, the seeds must ahve been planted and nurtured with care. What the writer needs to do is plant the clues to the ending throughout the story. As I said earlier I've only done this once and the short story was short, probably around 2000 words but to achieve the ending I'd envisioned the writing of this story took several months and many revisions to make sure all the clues were planted throughout.