St. Anselm's ontological argument, in its most succinct form, is as follows: "God, by definition, is that than which a greater cannot be thought. God exists in the understanding. If God exists in the understanding, we could imagine Him to be greater by existing in reality. Therefore, God must exist." A more elaborate version was given by Gottfried Leibniz; this is the version that Gödel studied and attempted to clarify with his ontological argument.
The first version of the ontological proof in Gödel's papers is dated "around 1941". Gödel is not known to have told anyone about his work on the proof until 1970, when he thought he was dying. In February, he allowed Dana Scott to copy out a version of the proof, which circulated privately. In August 1970, Gödel told Oskar Morgenstern that he was "satisfied" with the proof, but Morgenstern recorded in his diary entry for 29 August 1970 that Gödel would not publish because he was afraid that others might think
that he actually believes in God, whereas he is only engaged in a logical investigation (that is, in showing that such a proof with classical assumptions (completeness, etc.) correspondingly axiomatized, is possible).
Gödel died in 1978. Another version, slightly different from Scott's, was found in his papers. It was finally published, together with Scott's version, in 1987.
Morgenstern's diary is an important and usually reliable source for Gödel's later years, but the implication of the August 1970 diary entry—that Gödel did not believe in God—is not consistent with the other evidence. In letters to his mother, who was not a churchgoer and had raised Kurt and his brother as freethinkers, Gödel argued at length for a belief in an afterlife. He did the same in an interview with a skeptical Hao Wang, who says that
I expressed my doubts as G spoke [...] Gödel smiled as he replied to my questions, obviously aware that his answers were not convincing me."
Wang reports that Gödel's wife, Adele, two days after Gödel's death, told Wang that "Gödel, although he did not go to church, was religious and read the Bible in bed every Sunday morning." In an unmailed answer to a questionnaire, Gödel described his religion as "baptized Lutheran (but not member of any religious congregation). My belief is theistic, not pantheistic, following Leibniz rather than Spinoza."
Gödel left a fourteen-point outline of his philosophical beliefs in his papers. Points relevant to the ontological proof include
4. There are other worlds and rational beings of a different and higher kind.
5. The world in which we live is not the only one in which we shall live or have lived.
13. There is a scientific (exact) philosophy and theology, which deals with concepts of the highest abstractness; and this is also most highly fruitful for science.
14. Religions are, for the most part, bad -- but religion is not.