You're telling your friend a story about a funny thing that happened to you on the way to work. You've described leaving the house and starting to drive. Then you tell the funny thing that happened:
I was halfway there when I realized I didn't have my wallet.
This is a very common storytelling technique: you set up a continuous action you were doing, then you tell about a sudden event that happened. The suddenness can be expressed using the word "suddenly" before the subject:
I was walking down the street when suddenly I heard a loud bang behind me.
Or you can show the suddenness by using a verb that includes suddenness in its meaning, like "realize" in the example above.
To "realize" is to suddenly know something based on your own thinking - not based on what someone tells you or on things that you directly see.
In written English, it's considered correct to use "that" after "realize":
I realized that I didn't have my wallet.
But in spoken English, "that" is often dropped.
(Print this lesson)