What happens at the end of CLANNAD: After Story? This is a question that I really can't get my head around. I've asked many people but I still have not come to a good understanding of the ending of CLANNAD: After Story.

In the end of the anime, Nagisa and Ushio both come back to life. It's not terribly clear on how this happens and what causes it. Is it just magic?  

I found the ending very cryptic, so anyone could explain it well with relevant information and evidence of some sort, I'd be very glad!
I don't think this can be answered unambiguously even considering the visual novel, but it certainly explains things somewhat better than the anime in this case. In principle I can find quotes from the VN to support everything here, but that seems like a pretty arduous task given the size of the VN.

Of course, what follows constitute pretty major spoilers if you ever intend to read the VN regarding the illusionary world and the "lights", though . Some of this is not discussed in any way in the anime, so I've spoiler tagged everything (but if you don't ever intend to read the VN, there's no reason not to spoil yourself).


   In the visual novel, there are a number of "orbs of light" which are also in the anime but not focused on as much. Yukine describes them to Tomoya at the end of her route, and I believe also at some point in the anime. They are capable of granting wishes, but are fairly limited in power. They are created when someone helps someone else achieve a goal which brings happiness. For unknown reasons, Tomoya is one of the few people who can see them now, although they were visible to everyone in the past. In the game, Tomoya gets one orb for each completed route (8 total), with a couple of exceptions, and 5 more are available in After-Story. All of these are needed to reach the true end where Nagisa and Ushio don't die. 
After Tomoya collects the orbs, they are transported to the illusionary world, where Ushio (the true identity of the girl who lives there) keeps them. Ushio created the illusionary world in order to save herself and Nagisa by collecting the fragments of happiness that Tomoya gathers in the form of orbs of light. She is capable of doing so because "children are the culmination of their parents' hopes and dreams" (a theme that is repeated many times in the series). 
The only other people who are able to enter the illusionary world are those who willingly choose to do so, and Tomoya is the only person who falls into that category, so he is able to enter the body of a doll made out of garbage. Even when they made another doll, there were no more souls willing to enter the illusionary world, so it was lifeless. It isn't clear whether Ushio and Tomoya in the illusionary world have their real world memories, but Tomoya in the real world definitely doesn't remember the illusionary world, as evidenced by the fact that he thinks Nagisa's play is nostalgic but doesn't know why (Nagisa's play was about the illusionary world, although she probably also doesn't know this). It's also not clear how the chronologies of the two worlds works, but it's very likely that they are on completely different timelines. Judging by the fact that all of the illusionary world segments occur between days, he may be seeing it as a dream, but the research of the Ichinoses (Kotomi's parents, both of whom are theoretical physicists) indicates that the illusionary world is very much real and is connected to our world in many ways. 
In order to save Nagisa and Ushio, Tomoya needs to do 3 things. First, he needs to gather a lot of happiness in the form of orbs of light, enough to make such a big wish. Second, he needs to clearly tell Ushio (who is holding the orbs via her alter-ego in the illusionary world) that he doesn't want her or Nagisa to die and that he wants them to live together as a family. And third, he needs to really mean what he is saying, as the orbs won't grant wishes that aren't what one truly desires. If all of those are completed, the orbs of light will grant his wish, returning time to the point when Ushio was born and saving both Nagisa and Ushio. It should be mentioned that in order to collect these orbs, Ushio sends Tomoya to many different timelines, each time returning him to around the same point when the anime begins and resetting his memories. This is touched upon briefly in the special episodes revolving around Kyou and Tomoyo in which we see orbs of light at the end, meaning it's likely they're canon and actually happened during one of the timelines Tomoya was sent to.
Note also that this isn't the only time that the orbs of light grant wishes that would otherwise be impossible, although it's definitely the most powerful instance. When Nagisa is dying for the first time and Akio wishes to the tree that she doesn't die, that wish was also granted via the orbs. Also, when Misae declares her wish to Shima that she wants him to always be with her and love her, this is also granted, although unbeknownst to Misae, Shima was actually a cat and he returns to that form. In addition, depending on the order of routes that you choose in the VN, Tomoya may use an orb to pray for Fuko to get better, though if he does he'll be able to get it back in After Story. So this isn't as much of a deus ex machina as it might seem, but a legitimate plot point that mostly got swept under the rug in the anime.