I did not understand a thing from the last minutes of "A Scandal in Belgravia". Are we actually taking a look at Irene about to get killed, and is she hallucinating when she sees Sherlock? Or is Sherlock having a flashback? And if so, why did he receive the message only then in the present time.
It's actually quite a simple, albeit far-fetched ending.
Firstly, Dr. Watson comes into the apartment with the phone. Sherlock asks for it and this conversation occurs:
JOHN: Did she ever text you again, after ... all that?
SHERLOCK: Once, a few months ago.
JOHN: What did she say?
SHERLOCK: “Goodbye, Mr. Holmes.”
This obviously implies that she texted Sherlock goodbye and he never heard from her again. At this point, we as the audience are meant to believe this was their final communication and Sherlock is still upset/annoyed given her actions prior to this (with the entire Bond Air scandal).
Watson then leaves and Sherlock, once he's aware John is gone and can't see him scrolls through all the texts:
I’m not hungry, let’s have dinner.
Bored in a hotel. Join me. Let’s have dinner.
John’s blog is HILARIOUS. I think he likes you more than I do. Let’s have dinner.
I can see tower bridge and the moon from my room. Work out where I am and join me.
I saw you in the street today. You didn’t see me.
You do know that hat actually suits you, don’t you?
Oh for God’s sake. Let’s have dinner.
I like your funny hat.
I’m in Egypt talking to an idiot. Get on a plane, let’s have dinner.
You looked sexy on Crimewatch.
Even you have got to eat. Let’s have dinner.
BBC1 right now. You’ll laugh.
I’m thinking of sending you a Christmas present.
I’m not dead. Let’s have dinner.
Then comes the one reply he sent to her: Happy New Year
And at the bottom of the list is her last message to him: Goodbye Mr Holmes
Again, this implies that he got one last message from her and he's merely reflecting on it.
He looks out the window into the rain and we get Irene Adler's flashback. This is all done to show the story as Mycroft described it - her being killed by the terrorists. This all fits in with what we, the viewers, have been told, and so we accept it. It's moving, quite sad, and we see Irene send her final text before the screen goes to black...
... Only for us to hear the little groan that Sherlock's phone makes when he gets a text from her. Irene's eyes open and she realises that he is her supposed executioner, come to rescue her. He tells her to run and we see her smile.
Cut back to present day and Sherlock smiles away to himself thinking of The Woman.
The idea is that he rescued her. But he's Sherlock Holmes, the greatest detective in the world. No one knew he did it, not John or Mycroft or anyone else. And he didn't want them to know, so he didn't display any emotion regarding her in their presence (until he was alone at the end and smiled).
He didn't see the message at the end of the episode for the first time, he simply opened it to remind himself of the moment.
Irene is most definitely not hallucinating, but rather is being saved by Sherlock (although disappointingly, it seems unlikely she ever got her dinner date).
Co-creator and writer Steven Moffat clarified this:
It couldn’t have been [Sherlock] imagining it, could it? Because what he’s been told is that she’s in witness protection.
It is a very complicated train of thought for you to believe that Sherlock was imagining that.
No, he really goes and saves her. How could you doubt that?! Of course he’s going to save her!
It's a flashback.
Earlier in the episode Mycroft says "It would take a Sherlock Holmes to fool me". With the benefit of hindsight, this sets things up.
BTW, the message "BBC1 right now. You’ll laugh" shows the time and date that this episode was first shown.
Who says that script writers don't have a sense of humour.
SHE DID NOT DIE! I say thus because Irene is about to get executed by terrorists, and she sends her last message to Sherlock, "Goodbye Mr Holmes". (Btw she's NOT hallucinating. This is real what I'm about to tell you.) She then hears the ringtone of the phone, and looks up at the person about to execute her, when she suddenly sees, it's Sherlock!
Now there are a lot of people who didn't come to this website and just assumed she was hallucinating and died, I don't know, thinking happy thoughts before her death, NO. It's real people. Face life.
So Sherlock saves her and swings his sword (probably giving her time to run, but she simply smiles. But, this is a flashback, so she didn't get killed. Sherlock's having a flashback of this WHOLE thing!
Near the end of S04E02 (around 1:21), John hears Sherlock receiving a text message sound alert that sounds like the one from Irene Adler. It is very strongly suggested that Irene Adler is still alive.
Irene’s text alert
MARY: That noise, that’s a text alert noise.
JOHN: What was that?
SHERLOCK: Hm? What was what?
MARY: That’s the text alert of Irene Adler; she’s the scary mad one, right?
JOHN: That noise.
SHERLOCK: What noise?
MARY: She’s dead. Ooh, I bet she isn’t dead, I bet he saved her! Oh my God! Oh, the posh boy loves the dominatrix! He’s never knowingly under-cliche, is he?
JOHN: I’m going to make a deduction.
SHERLOCK: Oh, okay, that’s good.
JOHN: And if my deduction is right, you’re going to be honest and tell me, okay?
SHERLOCK: Okay. Though I should mention that it is possible for any given text alert to become randomly attached to-
JOHN: Happy birthday.
SHERLOCK: Thank you, John. That’s really kind of you.
JOHN: Never knew when your birthday was.
SHERLOCK: Now you do.
JOHN: Seriously? We’re not going to talk about this?
SHERLOCK: Talk about what?
JOHN: I mean, how does it work?
SHERLOCK: How does what work?
JOHN: You and ‘the woman’. Do you go to a discreet Harvester sometimes? Is there nights of passion in High Wycombe?
SHERLOCK: Oh for God’s sakes! I don’t text her back.
JOHN: Why not? You bloody moron! She’s out there, she likes you, and she’s alive! Do you have the first idea how lucky you are?! Yes, she’s a lunatic, she’s a criminal, she’s insanely dangerous. Trust you to fall for a sociopath!
MARY: Oh, married an assassin!
JOHN: But she’s… you know…
JOHN: Just text her back!
JOHN: Because High Wycombe is better than you are currently equipped to understand!
SHERLOCK: I once caught a triple poisoner in High Wycombe.
JOHN: That’s only the beginning, mate.
SHERLOCK: As I think I have explained to you many times before: romantic entanglement, while fulfilling for other people-
JOHN: Would complete you as a human being.
SHERLOCK: That doesn’t even mean anything.
JOHN: Just text her. Phone her. Do something while there’s still a chance because that chance doesn’t last forever. Trust me, Sherlock, it’s gone before you know it. Before you know it.