The key to successful surprise endings is to avoid tricking readers. If they're left out of the delight of discovery in an unexpected ending, they'll feel duped. It's also important to make sure the ending is relevant to the action of the story.
Let's say the story you describe features Polly, who is twenty-something and trying to prove her unsupportive parents wrong in their assumption she won't make much of herself in life. She loses her job as a receptionist at an accounting firm and begins to spiral into destructive behavior. She stays out late, drinks too much, and ends up in compromising situations with men she doesn't know. She stops paying rent and is days away from eviction. But Polly is sympathetic. She's earnest and eager for approval from her parents. She genuinely wants to make her own way, but the temptation to give up is growing. There's a message on her machine for an interview at the local library. You care about Polly and want to see what happens.
Then, you turn the page and find the story isn't going to give you any indication. Instead, there's that author finishing up his writing day. What happened to Polly? Why did you invest in this character only to be denied any sort of resolution? How is that author's action relevant to Polly's conflict? It feels like bait and switch: Polly lured you in but the story doesn't follow through.
Now, this doesn't mean that any story with that particular end scene will fail. To make it work, you'd need to write in a way that prepares the reader for the shift and shows how that shift is an important outcropping of the action that came before it. If it's apparent early on that the story isn't only about Polly, for example, readers know to invest their emotional energy elsewhere. It's your job to make that clear through emphasis, hints, or even direct reference.
Anton Chekhov's "A Trifle From Life" is a great example of how a story can be turned on its head and remain relevant without tricking the reader. In this story, Belyaev lives with Olga, who has an eight-year-old son, Alyosha. Belyaev learns that Alyosha has been seeing his father behind Olga's back. Alyosha swears Belyaev to secrecy before spilling the details, which include the father's thoughts on Olga's relationship with Belyaev. This makes Belyaev angry and jealous and when Olga arrives home, Belyaev tells her of the boy's meetings.
The story focuses on Belyaev's experience, but the end shines a sharp light on Alyosha's feelings, showing that the situation is not about the adult concerns—as we often assume in life—but about the child's deeply felt betrayal. Chekhov prepares the reader by showing Alyosha's perspective early in the story. He also reveals Belyaev's attitudes so readers know that sharing this secret with the jealous lover makes Alyosha quite vulnerable. There's just enough to let the reader know Alyosha's experience is important, but not so much to give away how or why until the discovery at the end.
The power and success of an ending isn't only in what happens, but also in how you finesse the details to prepare the reader.