It is indeed possible for an atheist or agnostic person to have a moral code that they follow without the guidance of any religion. Atheists can have certain moral principles that they uphold and do so with consistency, believing that there is good to be done and evil to be avoided. Some may not use the terms "good" or "evil," but "human" or "inhumane" or other equivalents. There are in fact many atheists and agnostics who are more morally upright and disciplined than many baptized Christians. So it is possible to have a morality without any kind of religion.
The question though is what is the foundation for an atheist’s morality? From an atheistic point of view the world as we know it and everything in it came into existence by happenstance and all life evolved from lower life forms due to the biological processes of evolution. There is no ultimate purpose, order, or meaning to the universe, it all just happened. And if there is no ultimate purpose, order, or Orderer that brings forth some kind of spiritual and moral order to these biological processes, doesn't it become a lot harder to ground one’s morality rationally and consistently
Problem 1 Determinism: if there is no spiritual component to human persons, who are only the result of the biological and chemical processes of evolution, this means that every action that is made by a human person is only the result of animal instinct, acting and reacting to certain stimuli without any real moral choice. Moral reasoning is only chemicals acting and reacting to external or internal stimuli in the body that cause it to act. This eliminates choice. If there is no choice, how can actions be good or evil if everyone must do what their own biological make-up decides for them? Or is there an explanation for how human persons developed the ability to make moral choices?
Problem 2 Survival of the Fittest: if we are all the result of an anomaly in nature and we developed due to evolution by means of the survival of the fittest, could this mean then that some humans or groups of humans may be more evolutionarily advanced than others with varying thoughts or worldviews of how we are best to evolve and adapt as a species to our world situation? Would enslaving or getting rid of humans who are less evolutionarily advanced be a way of ensuring the survival of the fittest? Or is there something that makes every human life objectively good that would suggest this kind of behavior is wrong or evil? Is "good," "evil," "human," or "inhumane," just the current majority's position now, but there is no overall basis in reality for what makes these actions really good or really bad?
Problem 3 Rational Animals but still Animals: If humans evolved from animals without anything else besides their rational ability to give them any more dignity or worth than any other animal, why then would it be wrong for a man to kill children of another family? Male lions kill the cubs of a new pride they join, but we do not have a moral problem with that. Is there a difference in actions qualitatively from other animals or are they essentially the same? Why or why not?
This obviously does not fully show the extent of an atheist's argument for why they believe there is a morality, nor does it give them the chance to respond to these questions, which they may have reasonable responses to. It's just meant to scratch the surface to some of the issues that could stem from having a morality not based in any type of religion.
From a Catholic perspective we would propose that God has written the natural law on every person’s law so that we can know generally what actions are good and what actions are evil. This does not automatically mean that God exists, it just means that our morality is based on something other than simply human thought and so is grounded in something more eternal than current social standards.
The Catechism of the Catholic Church states,
1956 The natural law, present in the heart of each man and established by reason, is universal in its precepts and its authority extends to all men. It expresses the dignity of the person and determines the basis for his fundamental rights and duties:
For there is a true law: right reason. It is in conformity with nature, is diffused among all men, and is immutable and eternal; its orders summon to duty; its prohibitions turn away from offense . . . . To replace it with a contrary law is a sacrilege; failure to apply even one of its provisions is forbidden; no one can abrogate it entirely.
1959 The natural law, the Creator's very good work, provides the solid foundation on which man can build the structure of moral rules to guide his choices. It also provides the indispensable moral foundation for building the human community. Finally, it provides the necessary basis for the civil law with which it is connected, whether by a reflection that draws conclusions from its principles, or by additions of a positive and juridical nature.For Catholics, the moral law is rooted in an eternal Lawgiver who made humans in His own image and desires that they fulfill the good for which they were created so that they can share in His happiness.
The Catechism of the Catholic Church states,
1954 Man participates in the wisdom and goodness of the Creator who gives him mastery over his acts and the ability to govern himself with a view to the true and the good. The natural law expresses the original moral sense which enables man to discern by reason the good and the evil, the truth and the lie:
The natural law is written and engraved in the soul of each and every man, because it is human reason ordaining him to do good and forbidding him to sin . . . But this command of human reason would not have the force of law if it were not the voice and interpreter of a higher reason to which our spirit and our freedom must be submitted.
1955 The "divine and natural" law shows man the way to follow so as to practice the good and attain his end. The natural law states the first and essential precepts which govern the moral life. It hinges upon the desire for God and submission to him, who is the source and judge of all that is good, as well as upon the sense that the other is one's equal. Its principal precepts are expressed in the Decalogue. This law is called "natural," not in reference to the nature of irrational beings, but because reason which decrees it properly belongs to human nature:
Where then are these rules written, if not in the book of that light we call the truth? In it is written every just law; from it the law passes into the heart of the man who does justice, not that it migrates into it, but that it places its imprint on it, like a seal on a ring that passes onto wax, without leaving the ring. The natural law is nothing other than the light of understanding placed in us by God; through it we know what we must do and what we must avoid. God has given this light or law at the creation