The novel ends, as it begins, with the painting. Dorian is finally forced to come to terms with his actions, and reaches a moment of crisis – is it too late for him to become good again and reclaim his innocence? After a fitful night of soul searching, the answer he reaches is no – it's too late to turn back from the path he's chosen. Furthermore, the image in the portrait reflects a new hypocritical side of his nature; even thinking about changing his ways was a denial of his true soul (which is rotten to the core).
Finally, Dorian attempts to destroy the portrait, the image of his disgustingly corrupted soul, which haunts him like a conscience. He slashes at it with a knife (appropriately the very same knife with which he murdered his ex-friend, Basil Hallward), hoping to do away with the evidence of his crimes. But the plan backfires dramatically – stabbing the portrait, Dorian inadvertently kills himself. The grotesque deformities of the picture come into being in Dorian's own body, while painted Dorian is restored to its original image of spotless beauty. In the end, Dorian gets everything that was coming to him; his choices brought about his own doom.
Questions like "Why?" and "How?" aren't really ours to apply to this ending – the magical element of this story is just one of those things that we're asked to believe. What really matters about it is not its fairy-tale-gone-wrong turn of events, but rather the message that it conveys. The idea here is that nobody can get away with everything; even though Dorian thought that he could dodge earthly punishment and go about his evil business by destroying the portrait (the proof of how vile and corrupt he really was), his death actually comes as a kind of divine retribution for all of his crimes.
Notably, the painting is restored to its original pristine state by this act – this goes back to the statement Wilde makes about art in the "Preface." The artwork is totally removed from questions of good or evil – once Dorian's corrupt life-force is lifted from it, the painting reverts to its natural state of beauty, without a moral stance.