William Golding wrote Lord of the Flies in part as a response to the Victorian novel Coral Island by R. M. Ballantyne. In that book, British schoolboys manage to transform a deserted island into a miniature Britain, a civilized and respectable place. Golding aimed to show that such an outcome was unrealistic due to the tendency toward moral darkness that plagues the human race. Lord of the Flies chronicles the descent of the boys from order to chaos, from morality to immorality, and from civilization to savagery. The natural destination of such a journey would be complete annihilation, and Golding brings the boys very near to that terminus.
However, just as the fire the boys have started threatens to consume the island and everything on it, a British Naval ship appears, and the boys are rescued in the nick of time. There could be several reasons Golding chose to end the book as he did. First, since it is a book about children, having them all die at the end would seem especially dark. Readers desire happy endings, and when authors become known as providing depressing fare, their readership often dwindles. Golding's work may have been read by fewer people and appreciated less if he had killed off all the characters. Second, although the novel is a warning, Golding did not mean to imply that humans are beyond hope. He shows that some people can remain true to civilization and morality, which means that it is possible to fight the moral darkness within. Having the children rescued suggests that humanity still has time to change its war-like ways. Third, taken symbolically, the Naval officer can represent salvation. For a person of faith, the officer can point to the belief that God is capable of redeeming mankind and rescuing him from the mess he has made of his personal life and of the world at large. For a person who believes salvation can come through education, enlightenment, or other means, the Naval officer can represent transformational movements that will usher in an era of higher understanding and peace.
By ending the novel as he did, Golding was able to forcefully present his themes about evil and how people relate to each other while still leaving readers with a sense of hope for the future.