The ending is quite unclear, and this was almost certainly a deliberate choice made by Stephen King, in order to allow the reader decide for himself/herself. As a result, any attempt to explain it will necessarily involve a large amount of speculation. As such, I feel entitled to offer my own interpretation.
My understanding has always been that Roland has repeated his quest any number of times - we see this pretty clearly in his last words before being pulled through the door: "Not again!" We might call each of Roland's repetitions of his quest a "cycle". The first time Roland began a cycle, he did so of his own free will. He chose to seek the Tower. But he did everything wrong along the way. As a result, he was doomed to repeat the cycle until he got it right. From this point onwards, the quest is also a punishment. He must repeat it again and again, until he finally learns from his mistakes.
What mistakes, you might ask? His most glaring flaws should be obvious already: he treats other members of his Ka-tet as objects or tools for him to use and dispose of in whatever way, and at whatever time he sees fit.
This is especially apparent in the first edition of the first book, when he allows Jake to die, because it would inconvenience him to save the boy's life. He has a choice between saving Jake or chasing the Man in Black; he might be able to do both, but it would delay him a bit, so he lets the kid die. In the same book, although only in the first edition, he kills the woman he has been living with, and again, he does so because it is the easiest way to solve his problem.
It is likely that, in the cycle of the quest we read about in the series, Roland was more compassionate towards the other members of the Ka-tet than he had been in previous cycles, but he still made a fair share of mistakes. He was still too willing to let the others die, or risk their lives, and he didn't show enough concern for their wellbeing.
He still treated his companions as tools, as means to an end, not as human beings with as much right to live as himself. He still let Jake fall to his death. He still used the other members of the Ka-tet rather than treating them as equals. He still didn't understand that he is not the center of the universe.
However, there is at least some good news for the Gunslinger.
In each previous cycle of the quest, we are led to believe, Roland was missing something important to his mission. In the cycle we read about in the series, he doesn't have the horn of Arthur Eld. As the book ends, and Roland finds himself being pulled through the doorway yet again, he does have the horn. As I interpret the text, it was my understanding that this fact implies that the next cycle of the quest will be the last.
Whatever Roland did wrong this time through the cycle, it wasn't as bad as the stuff he did wrong all the previous times he completed a cycle. The next time he makes a cycle, he will have done everything right. He will deserve his reward: to find what he has been looking for all along; to finally be done with this endless of loss and suffering; to die with dignity; to be at peace with himself and the world.
How might the next cycle be different from all the previous cycles? We don't really know - it seems likely that he has to do something differently than he has in the past. Maybe Roland was supposed to sacrifice himself, rather than everyone else in the Ka-tet. Maybe he was supposed to keep everyone in the Ka-tet alive. Maybe he doesn't have to do anything differently. Maybe he got enough things right this time, and next time will be a cake walk. We don't know.
The ending of the series was a matter of heated debate among the rabid fan base, and there were plenty of people who were furious about how inconclusive it seemed to be. I was surprised by how it ended, and at first, I might have been a bit disappointed. But shortly after I finished reading the final volume, I came to realize that the ending, while not entirely satisfying, was perfect, and it was exactly what it had to be.
On the one hand, it makes sense in the context of the story, because his flaws are so incredibly obvious. He keeps screwing up, so he doesn't deserve to fulfill his quest. On the other hand, it gives us, the audience, an excuse to read the whole thing again - if Roland has to do it all over, why shouldn't we?
I don't know of any other story in which you can read it again and have it make perfect sense according to the logic of the story. No matter how many times you read The Lord of the Rings, you know that the events in the story only happened once. But with The Dark Tower, you can imagine that you are reading about another cycle of Roland's quest. Stephen King managed to create a truly never-ending story.