Here are quotations from Freud about the psychological origins of religious and ethical behaviour:

Freud on projection and illusions

And thus a store of ideas is created, born from man’s need to make his helplessness tolerable… It can clearly be seen that the possession of these ideas protects him in two directions – against the dangers of nature and Fate [including death], and against the injuries that threaten him from human society itself… Over each one of us there watches a benevolent Providence which is only seemingly stern and which will not suffer us to become a plaything of the overmighty and pitiless forces of nature. Death itself is not extinction, is not a return to inorganic lifelessness, but the beginning of a new kind of existence which lies on the path of development to something higher… [Moral laws] are maintained by a supreme court of justice with incomparably more power and consistency. In the end all good is rewarded and all evil punished, if not actually in this form of life then in the later existences that begin after death. The Future of an Illusion, 1929, pp.18-19

The child’s attitude to its father is coloured by a peculiar ambivalence... It fears him no less than it longs for him and admires him. When the growing individual finds that he is destined to remain a child for ever, that he can never do without protection against strange superior powers, he lends those powers the features belonging to the figure of his father; he creates for himself the gods whom he dreads, whom he seeks to propitiate [appease], and whom he nevertheless entrusts with his own protection. Thus his longing for a father is a motive identical with his need for protection against the consequences of his human weakness. The Future of an Illusion, 1929, p.24

‘Well then, if even obdurate sceptics admit that the assertions of religion cannot be refuted by reason, why should I not believe in them, since they have so much on their side – tradition, the agreement of mankind, and all the consolations they offer?’ Why not, indeed? Just as no one can be forced to believe, so no one can be forced to disbelieve. But do not let us be satisfied with deceiving ourselves that arguments like these take us along the road of correct thinking. If ever there was a case of a lame excuse we have it here. The Future of an Illusion, 1929, p.32

[Religious ideas] are illusions, fulfilments of the oldest, strongest and most urgent wishes of mankind. The secret of their strength lies in the strength of those wishes. As we already know, the terrifying impression of helplessness in childhood aroused the need for protection – for protection through love – which was provided by the father; and the recognition that this helplessness lasts throughout life made it necessary to cling to the existence of a father, but this time a more powerful one. Thus the benevolent rule of a divine Providence allays our fear of the dangers of life; the establishment of a moral world-order ensures the fulfilment of the demands of justice, which have so often remained unfulfilled in human civilisation; and the prolongation of earthly existence in a future life provides the local and temporal framework in which these wish-fulfilments shall take place. Answers to the riddles that tempt the curiosity of man, such as how the universe began or what the relation is between body and mind, are developed in conformity with the underlying assumptions of this system. The Future of an Illusion, 1929, p.30

Illusions need not necessarily be false – that is to say, unrealisable or in contradiction to reality. For instance, a middle-class girl may have the illusion that a prince will come and marry her. This is possible; and a few such cases have occurred. That the Messiah will come and found a golden age is much less likely… We call a belief an illusion when a wish-fulfilment is a prominent factor in its motivation… The Future of an Illusion, 1929, p.31

Freud on the super-ego

What happens in him [an individual] to render his desire for aggression innocuous?… His aggressiveness is introjected, internalised; it is, in point of fact, sent back to where it came from – that is, it is directed towards his own ego. There it is taken over by a portion of the ego, which sets itself over against the rest of the ego as super-ego, and which now, in the form of ‘conscience’, is ready to put into action against the ego the same harsh aggressiveness that the ego would have liked to satisfy upon other, extraneous individuals. The tension between the harsh super-ego and the ego that is subjected to it, is called by us the sense of guilt; it expresses itself as a need for punishment. Civilization and its Discontents, 1930, p.123

If he loses the love of another person upon whom he is dependent [a parent or the ‘larger human community’ or even the super-ego], he also ceases to be protected from a variety of dangers. Above all, he is exposed to the danger that this stronger person will show his superiority in the form of punishment. At the beginning, therefore, what is bad is whatever causes one to be threatened with loss of love. For fear of that loss, one must avoid it. Civilization and its Discontents, 1930, p.124

If the id in a human being gives rise to an instinctual demand of an erotic or aggressive nature, the simplest and most natural thing is that the ego, which has the apparatus of thought and the muscular apparatus at its disposal, should satisfy the demand by an action. This satisfaction of the instinct is felt by the ego as pleasure, just as its non-satisfaction would undoubtedly have become a source of displeasure. Now a case may arise in which the ego abstains from satisfying the instinct in view of external obstacles – namely, if it perceives that the action in question would provoke a serious danger to the ego… In the course of an individual’s development a portion of the inhibiting forces in the external world [e.g. society, parents, praise and punishment] are internalised and an agency is constructed in the ego which confronts the rest of the ego in an observing, criticising and prohibiting sense. We call this new agency the super-ego. Thenceforward the ego, before putting to work the instinctual satisfactions demanded by the id, has to take into account not merely the dangers of the external world but also the objections of the super-ego, and it will have all the more grounds for abstaining from satisfying the instinct. But whereas instinctual renunciation, when it is for external reasons, is only unpleasurable, when it is for internal reasons, in obedience to the super-ego, it has a different economic effect. In addition to the inevitable unpleasurable consequences it also brings the ego a yield of pleasure – a substitutive satisfaction, as it were. The ego feels elevated; it is proud of the instinctual renunciation, as though it were a valuable achievement. We believe we can understand the mechanism of this yield of pleasure. The super-ego is the successor and representative of the individual’s parents (and educators) who had supervised his actions in the first period of his life; it carries on their functions almost unchanged. It keeps the ego in a permanent state of dependence and exercises constant pressure on it. Just as in childhood, the ego is apprehensive about risking the love of its supreme master; it feels his approval as liberation and satisfaction and his reproaches as pangs of conscience. When the ego has brought the super-ego the sacrifice of an instinctual renunciation, it expects to be rewarded by receiving more love from it. The consciousness of deserving this love is felt by it as pride. Moses and Monotheism, 1939, pp.116-7

Religion as a neurosis; omnipotence of thoughts

I have adopted the term 'omnipotence of thoughts' from a highly intelligent man who suffered from obsessional ideas and who, after having been set right by psycho-analytic treatment, was able to give evidence of his efficiency and good sense [The case of the 'Rat Man', Notes upon a Case of Obsessional Neurosis, 1909, p.233ff.]. He had coined the phrase as an explanation of all the strange and uncanny events by which he, like others afflicted with the same illness, seemed to be pursued. If he thought of someone, he would be sure to meet that very person immediately afterwards, as though by magic. If he suddenly asked after the health of an acquaintance whom he had not seen for a long time, he would hear that he had just died, so that it would look as though a telepathic message had arrived from him. If, without any really serious intention, he swore at some stranger, he might be sure that the man would die soon afterwards, so that he would feel responsible for his death. In the course of the treatment he himself was able to tell me how the deceptive appearance arose in most of these cases, and by what contrivances he himself had helped to strengthen his own superstitious beliefs. All obsessional neurotics are superstitious in this way, usually against their better judgement. Totem and Taboo, 1913, p.85-86

If we are prepared to accept the account given above of the evolution of human views of the universe – an animistic phase followed by a religious phase and this in turn by a scientific one – it will not be difficult to follow the vicissitudes of the 'omnipotence of thoughts' through these different phases. At the animistic stage men ascribe omnipotence to themselves. At the religious stage they transfer it to the gods but do not seriously abandon it themselves, for they reserve the power of influencing the gods in a variety of ways according to their wishes. The scientific view of the universe no longer affords any room for human omnipotence; men have acknowledged their smallness and submitted resignedly to death and to the other necessities of nature. None the less some of the primitive belief in omnipotence still survives in men's faith in the power of the human mind, which grapples with the laws of reality. Totem and Taboo, 1913, p.88

… it is impossible to escape the impression of the perfect correspondence which can be discovered between the obsessive actions of certain obsessional patients and the religious observances of believers all over the world. Some cases of obsessional neurosis actually behave like a caricature of a private religion, so that it is tempting to liken the official religions to an obsessional neurosis that has been mitigated by becoming universalized. This comparison, which is no doubt highly objectionable to all believers, has nevertheless proved most fruitfully psychologically. A Short Account of Psychoanalysis, 1924