The Future of an Illusion (1927) by Sigmund Freud deals with the question of religion's origins and its underlying psychological structure. Freud holds the religion is an illusion aimed at managing men's emotional needs going back to early childhood.
in Freud's eyes in "The Future of an Illusion" religion is an illusion built on "certain dogmas, assertions about facts and conditions of external and internal reality which tells one something that one has not oneself discovered, and which claim that one should give them credence". The adherence to religion first arises out of the inheritance received from previous generations and the continuity of faith (Marx had something else to say about this). Secondly, since we hold the faith to be true, we also believe that proofs to its veracity are true (yup, circular). Thirdly, if religion is true and so are the proofs attesting to its truthfulness you are forbidden to doubt any of it.
But for Freud there is something deeper about religion that has to do with wish fulfillment, which he holds is at the base of religion's illusion. Religion fulfills the "oldest, strongest, and most urgent wishes of mankind". These deep wishes include, for example, clinging on to the father, prolonging life and attaining immortality.
Though built on wish fulifilment religion from Freud is in the end a system of repression designed to fend off an control wild indevidual desires and urges that threaten society. Since human nature, for post WWI Sigmund Freud, is animalistic and destructive in its core it is necessarily for societies to develop civilizing systems to prevent chaos.
In an important part of "The Future of an Illusion" Freud ties the development of religion with the Oedipal Complex. God, like the father, serves to salvage the child's sense of helplessness against nature and fear of death. The father/god offers the illusion of wish fulfillment in exchange to obedience and subjection.