The lore of Terraria refers to the game's back-story.
While Terraria was initially released without a clear story component, its developers at Re-Logic wrote and released a story connecting its characters and gameplay elements in celebration of the 8th anniversary of Terraria's release. The developers have mentioned that this is intended to be one possible interpretation of the lore, and that players are encouraged to piece together a story (or lack thereof) as they see fit. Regardless, many entries in the Bestiary contain matching pieces of lore.
Official lore pages
Welcome to the World of Terraria. A land full of mystery and wonder, with much of its destiny left up to the wildest imaginations... however, a few legends of old have made their way to our present time. Passed down by countless generations of the Order of the Guide, these few scraps of information will serve to help you navigate your journey and overcome threats lurking in the shadows. This knowledge is now passed on to you, the brave adventurer, with the hope that it will aid you in your quest to save our world...
In the beginning, the Gods established a balance to guarantee fairness for all living creatures. This balance was to be of paramount importance, with no cost too great in seeking its fulfillment. Eons have passed since the Gods first began testing the complexities of life in their fairness experiment. In this process, countless living worlds were created and now exist within the Terraria universe. Each world of Terraria is a sentient being that knows all that goes on and can feel the thoughts of all living creatures: with their sole purpose being to ensure that the desired balance is maintained – often violently so. The worlds utilize many defense mechanisms to ensure the balance is upheld at all costs – some of them are known as the Hallow, Corruption, and Crimson.
The Crimson is a single emergent living being connected directly to each world, sharing a hive mind, and solely focused on restoring balance at all costs. Thousands of worlds before the one on which you stand now have been absorbed by this being. Many misguided people have made the horrific mistake of raising the Crimson to the level of a deity – conducting human sacrifices to it to placate the monstrosity or seek its favor. The Crimson gladly consumes these bodies, becoming one with them and producing terrifying beings, who lose the ability to feel and blindly follow the hive mind.
The Corruption is a cancer caused by the sins of those living in the worlds of Terraria. The vile actions and thoughts present in all beings feed the growth of the Corruption as it spreads relentlessly across each world. The Corruption knows nothing else but to consume everything it touches, leaving behind terrible creatures of hate that exist for the sole reason to cause pain in punishment for the unearned pleasure experienced by living things. After the Corruption restores balance to life, it destroys it with the goal of turning the world into a desolate abyss void of life.
The Hallow, on the other hand, is of an entirely different nature. Within each world is a Guardian who serves as the worlds[sic] master and core. Once this creature is destroyed the world will release the Ancient Spirits of Light and Dark to expedite the process of finding a new Guardian. It is here when the Hallow is created and functions as an overcompensation of purity, taken to the absolute extreme. The Hallow cures threats that would attempt to violate the critical balance of life, killing anything in its path as though it were treating an infection – whether friend, foe, or neutral party. Ultimately, the Hallow serves to push back against the never-ceasing encroachment of control.
It is against the backdrop of this precious balance of life, desolation and pain that the great battles of legend and the adventures of our time take place. In the blind spot of the universe, there exists a planet of unprecedented potential: your world!
Your story begins with Cthulhu – a creature of immeasurable power and unknown origin – who arrived long ago with its seemingly sole purpose being to rain destruction on and to have dominion over all the sentient life that flourishes on your world.
None could stand against the advances of Cthulhu. The very fabric of Terraria itself seemed on the precipice of doom. At last, when all hope seemed lost, the ancient race of Dryads arose to wage battle against Cthulhu. The Dryads, with their unparalleled connection to the planet, were Terraria's truly last hope as they joined together to save all life from annihilation.
The Dryads were alas unable to kill Cthulhu. However, with the Dryads [sic] combined power they were able to cripple Cthulhu's ability to wreak further damage on Terraria by ripping out Cthulhu's eyes, part of its skeleton, and chunks of its brain. Ultimately this substantial damage forced Cthulhu to retreat to the dark side of the moon, where it dwells to this day, gathering strength for another attempt at total conquest. As for the Dryads, sadly, all but a single member perished... and the sole survivor has not been seen for many, many years.
Much time has passed since the Great War of Cthulhu. However, rumors tell of a Lunatic Cult led by a fanatical zealot that is methodically seeking to revive Cthulhu to its former power and bring about the end of the world. As a part of these efforts, a renowned genius – known simply as the Mechanic – has been kidnapped and forced to rebuild the parts needed to make Cthulhu whole once again. Hushed whispers tell of the results of these experiments: horrible mechanical simulacrums of Cthulhu's organs. The Mechanic has nearly completed her work, only needing to finish the Mechanical brain to restore Cthulhu to its full destructive power.
Passing travelers hear construction and screams emanating from the Dungeon – an evil, demonic fortress of the dead in which the Mechanic is supposedly held prisoner as she conducts her work.
Many across Terraria have fallen under the possession of the cult, including the benevolent Old Man who oversees the Dungeon, a once thriving city full of life until a curse forced all its inhabitants to go mad – living beyond the point where their bodies rotted away and they became mindless undead servants of evil. Without some intervention, some force to stem this tide, some hero to save the day, Terraria's doom is nigh at hand!
This brings us to our adventure. It begins with you – in humble beginnings and shown your path by the faithful and mysterious Guide. As this world's champion, you will experience communications from the world to help protect your mission. The world has brought you to this specific location at this specific time – will you stand up and fight for Terraria against the growing shadows of impending doom?