So, a few things before I answer this:
I know I am late
SPOILER ALERT
Here goes...
About the orbs of light
The orbs of light appear when a particular goal is achieved. It has the ability to grant wishes.
About alternate timelines
In Clannad, there are alternate timelines where, obviously, things are different. In the OVAs, Tomoya dates Kyou and Tomoyo. These events take place in the alternate timeline. Kotomi's parents were performing a research on the alternate world.
About the Illusionary world
In theory, the Illusionary World is the afterlife; when a person dies they are still connected to the real world, yet they are free to create their own world.
The Illusionary World in Clannad was created by Ushio. In the Illusionary World the girl is Ushio and the doll is Tomoya. Tomoya also mentions in an episode that he somehow knows the end of the story that Nagisa told him. The story was about the Illusionary World.
The ending
We see that Tomoya had a miserable life. Nagisa and Ushio die, and it wasn't going his way. Later on, Tomoya goes back to the point where he first met Nagisa, and then he marries her and they have a kid. But this time Nagisa survived.
Now there are three possible interpretations:
Tomoya was just dreaming of the worst that could happen. But in the end nothing bad happens and everything is okay.
But if this would've happened, then the orbs of light have no role. And the scene where he goes back to the past wouldn't have made sense.
Nagisa survived in an alternate timeline. But again, the orbs of light have no role.
The scene where he goes back wouldn't make sense either because in that scene Tomoya and Nagisa remember each other... And it wouldn't have been possible even if they were in the alternate timeline.
This is probably what had happened.
So in the illusionary world we see the orbs of light every now and then. These were collected by Ushio (the girl) and Tomoya (the doll). Tomoya first wished that he shouldn't have met Nagisa. But it wasn't what he really wanted and thus it wasn't granted by the orbs light. What he really wished for was for Nagisa to be alive.
The orbs of light that Ushio collected in the Illusionary World granted this big wish and thus Tomoya is taken back to the past. He didn't say anything to Nagisa. He was in a dilemma here. Nagisa started walking away and at that moment Tomoya made his mind and ran to Nagisa and hugged her. They still remembered each other. Then he is taken to the point where Nagisa gives birth. She survives and they live happily ever after.
Now here both the conditions are satisfied.
The orbs of light grant him his wish
The scene where he goes back to the past makes sense because he has a faint memory of the terrible thing that happened to him (mentioned by Tomoya in the recap episode)
People can believe whatever they want. I personally feel the third one makes sense.
If you've made it this far then thank you for reading!
Clannad is really the best anime I've ever watched. Hopefully this clarified things :)
This is the ending: https://www.youtube.com/watch?v=Tc4MsZwBWOA