As in human cultures, in many continuities the Transformers have possessed religion to help them explain and cope with their place in the universe. In continuities where they exist, Transformer spiritual and religious beliefs are usually specifically illustrated to be truth rather than legend or superstition.
Generation One cartoon
In the original Generation One Cartoon, despite their witnessing supernatural occurrences such as magic and ghosts on a number of occasions without any apparent amazement or surprise there is little direct evidence for general Transformer spiritual or religious beliefs. Kup recommends praying before battle, but does not specify to whom.
It is known that the spirits of Autobot leaders past dwell within the Matrix of Leadership even after their deaths, but the mechanism of this is unknown. A living Autobot leader can journey within the Matrix to commune with them and receive visions of the past. (Rodimus needed to be near death to achieve this, but Optimus could instigate it at will.) It is unknown how spiritual or how mechanical this whole process is.
The best, if not only, other evidence we have comes from The Transformers: The Movie with the apparent prophecy that "one day an Autobot shall rise from our ranks, and use the power of the Matrix to light our darkest hour" and the repetition, prayer-like, of the phrase "'Til all are one."
Marvel Comics continuity
This is where the concept of Transformer religion and spiritual beliefs really took form and spread. Nearly all subsequent uses of Transformer religion derive from the foundation laid in these stories.
The story of the gods Primus and Unicron went through several iterations as they were retold by different speakers over the course of the US and UK comic run. The most distinct difference across these revisions was the fading out of their respective pantheons. When first told, by Unicron, Primus and Unicron were each merely part of a whole pantheon of other light and dark gods respectively. In the next version, by the Keeper, they were the last of their pantheons, the others already having been destroyed or passed on to the Omniversal Matrix. Finally, in the version told by Primus, they were each unique beings with the existence of other gods ignored or refuted. This final version is the most frequently borrowed by later material. (The Botcon "Reaching the Omega Point" story being the notable exception, see below.)
One legend surrounding Primus is that of the Last Autobot, Primus' final guardian who would come after the god's passing and the defeat of Unicron.
The Creation Matrix is the living essence of Primus' life force and considered a sacred artifact. The bearer of it is gifted with the power to create new Transformer life. Each Matrix bearer, known as a Prime, takes a derivation of the name Primus as part of his own. Five have existed: Prima, Prime Nova, Sentinel Prime, Optimus Prime, and Rodimus Prime. Though its existence was well known, the manifest physical nature of the Matrix was not, thus it was ignored by Shockwave when he captured Optimus and, following his death, was launched into space with Optimus' body by his fellow Autobots.
The Matrix Flame on Cybertron is set in a golden blazer on a green altar, and tended by a red tabard clad acolyte. It is the living embodiment of the sacred Creation Matrix. Its status indicates that of the Matrix and its bearer.
Several scriptual texts were recorded in the Covenant of Primus, believed to be prophetic. Only two copies existed.
After millennia of war, many Transformers regarded Primus and Unicron as mere legends or disregarded them entirely. On hearing the name Primus spoken Bumblebee claims to have almost forgotten the Transformers ever had a God. Even when confronted with a giant head at the center of their planet Grimlock does not at first accept the myth of Primus and Unicron as true.
On the other hand, Emirate Xaaron appears to have believed, and Optimus Prime, likely due to his being Prime, was quick to accept Primus and Unicron. Interestingly, though he is bearer of Primus's life force, Optimus was not one to unthinkingly and zealously follow his god, harboring, at times, serious misgivings and distrust about the use to which Primus was putting his children.
There were likely still some number of true believers, even prior to Primus' reemergence, as the tender of the Matrix Flame and the purple cloaked monk-like elders who aid Prime in the Generation 2 comics attest.
Botcon: "Reaching the Omega Point"
The only other Transformer God, aside from Primus and Unicron, to appear in any story shows up here: The Chronarchitect. He is presumably of Primus' pantheon (see Marvel Comics above) given his alliance. He is the god of time.
While exploring Cybertron's interior Dr. Brian Jones was transported to an long-abandoned temple where offerings of energon had been left for the legendary "guardian of energon" who had protected Cybertron long before the current generation of Transformers was created. Prior to witnessing Ancient Guardian's reactivation, Jones believed these stories were just myths. The Japanese version of this episode describes the Energon offerings as happening 'tens of thousands of years ago.'
The multiversal mythos
Beginning around the time of Transformers: Armada a new version of the Primus/Unicron story began to take shape. This new mythos was most notable for its multiversal nature: it applied not just to the new Unicron Trilogy continuity family but also to the new iterations of Generation One being created by Dreamwave and to the multiversal Beast Machines spin-off Transformers: Universe. In fact, by its very nature the new Multiversal Mythos attempted to apply itself to everything, even retroactively to past continuity where it was often a not-so-easy fit.
The basics of this new version of the myth are that both Primus and Unicron were created by "The One," an original God-being, and sent out into the universe as emissaries/explorers. Unicron goes bad and Primus must stop him. For some unexplained reason Primus exists in all possible continuities simultaneously (picture a single string stretching through many rooms) and his destruction in any one of them would destroy all creation, while Unicron, on the other hand, exists in only one Universe at a time ever. There are no "alternate reality" multiversal versions of Unicron, he's just one single God who leaps between universes (picture a ball of yarn thrown from room to room).
Needless to say this is a tricky bit of retconning that is hard to mesh with the original Cartoon (where there was no Primus and Unicron was only a robot made by Primacron) or the Marvel Comic where Primus is stated to have died and the multiverse doesn't implode.
Cult of Unicron
Some Transformers have willingly (or unwillingly) come to serve the dark god Unicron. The Fallen was the first of these.
In the Marvel Comic, before their long sleep Unicron was able to bury a powerful compulsion in the minds of a number of Transformers causing them to form a secret cult of Unicron, which attempted to slay Optimus Prime upon his being named the leader of Primus' forces.
The Decepticon myth of the Ultimate Warrior appears to be a corruption of the Last Autobot story. In the Marvel Comic the mystically inclined Decepticon Bludgeon dedicated his life to the "teachings" of the Ultimate Warrior, a path that may be taken as a possible alternate Cybertronian religion.
In Dreamwave's second War Within miniseries Bludgeon and other like-minded Decepticon mystics are shown to commune with and worship "dark" mystical forces in attempt to gain power.
The Japanese Generation One series present two characters that might also factor in to a Decepticon religion. Devil Z is described alternately as 'the god of the Destrons' and a guardian angel who's looked after/been worshiped by them. After Devil Z's destruction a new supernatural being arises to lead the Decepticons: Violen Jygar. A demon-like being said to be born from many angry Decepticon sparks, V.J. possessed the supernatural ability to raise the dead.
It has also been speculated that Megatron took his name from a verse from the Covenant of Primus.
Jetfire is often portrayed as being a Transformer atheist/agnostic due to his scientific worldview. In IDW's comic series he swears by Primacron rather than Primus (Although what that implies about said continuity beyond being a cute in-joke is unknown. Scoop swears by him later on in the same issue).