Back in the naughties, especially in New Atheist circles, you used to see the line a lot that the reason religious people invented the afterlife was because they were scared of dying and they needed a comforting lie to sleep better at night. Incidentally, that's not true; aside from the problem that people in the past generally believed in their religion, and this whole line of reasoning (along with "religion was invented solely to control the masses") assumes a level of cynicism by religious leaders that historically is actually quite rare, we have a pretty good cognitive framework for why human beings tend to come up with a belief in spirits, ghosts, and gods, and why that tends to lead to a belief in an immaterial spirit world and (quite naturally from there) an afterlife.
Research into the cognitive aspect of spiritual beliefs has explored human intuitions about the self include its partability and permeability, which I think I've mentioned here before; our intuitions about ascribing agency to phenomena in our environment, even when no agency is immediately evident (a sort of overly-cautious tripwire for evading predators) and our overactive tendency toward pattern-matching lend themselves naturally to belief in invisible, intelligent agents shaping the world around us. When you combine that natural tendency to believe in such agents, plus intuitions about a self that can include a separate immaterial component, and the ways in which (for example) the feeling of a familiar presence can be triggered by some stray bit of sensory input or a misinterpreted environmental cue, it is very common for societies to develop a belief that the dead continue to exist in some form and continue to act in the world, possibly from some invisible spirit realm, because that is something that people are just straightforwardly experiencing on a day-to-day basis. In that sense, belief in something like a soul and something like an afterlife is more like a belief in rainbows or solar eclipses--sure, people might get the underlying phenomenological explanation for what they're seeing wrong, but they're not speculating, they're doing their best to interpret the actual experience of feeling the presence of dead loved ones and their apparent agency in the world.
That said, in the case of Christianity, we also know historically the framework that motivated the development of specifically Christian doctrines about the afterlife, which emerges from the context of Second Temple Judaism at the turn of the era. Here, the motivation was not one of comfort stemming from fear of death, it was one of morality and the problem of evil. Earlier thinking in the sort of broader Levantine cultural sphere had mostly envisioned the problem of evil as being one related to divine favor and punishment; God or the gods rewarded the righteous and punished the wicked in this life (cf., for instance, all the narratives in the Old Testament where God sends this or that conqueror to punish the people for their sins). Increasing philosophical sophistication, literature grappling with the ways in which the world could be patently unjust (like the Book of Job), and political circumstances like the conquest of Judea by the Romans and the evident lack of divine retribution against these oppressors, all led to dissatisfication in some quarters with that earlier theodicy. IIRC the influence of Greek philosophy and Greek thinking about the afterlife also played a role here.
Transposing the balancing of the moral scales to the afterlife, as some Second Temple-era thinkers did, helped construct what felt like a more intuitively correct theodicy: the wicked still got their comeuppance, even if you didn't get to personally witness it, and the righteous still got their reward. The exact nature of that comeuppance was up for grabs for a long time--there are like three different competing visions of what damnation looks like in the New Testament, and it's not until later that "eternal conscious torment" wins out as the favored position among most Christians. The righteous were always guaranteed salvation; but we know this wasn't a sop to people who were frequently scared of death because the idea that martyrdom guaranteed salvation was so compelling you had Christians begging the Roman authorities to put them to death, and even groups like the Circumcellions who attacked armed soldiers with clubs in the hopes that they could provoke martyrdom-by-cop. And you could paint these guys as fanatical outliers, but again, people in the past generally believed their religions, and we have mountains of writing, art, poetry, and music by Christians over the course of two thousand years where people are worried about a lot of things related to death (did I live a good life? will I go to heaven?) but who do not seem to be philosophically troubled by the question of whether the afterlife actually exists.
And of course the conflict between reflective and intuitive cognition is relevant here; one might reflectively believe in the afterlife, but intuitively recoil from deadly harm. I do not want to suggest that religious belief can trivially overwhelm human instinct to survive. But "the afterlife was invented as a comforting lie" is overly dismissive and flattens a complex phenomenon. It is, in its own way, a comforting lie--the lie that people in the past were all stupid, superstitious rubes, that we are infinitely smarter and more sophisticated than them, that progress will ultimately consign all such supernatural thinking to the dustbin of history. That such thinking is quite deeply rooted in our cognition and we may never be able to dispense with it entirely is very much at odds with a lot of the 2000s era all-religion-is-indoctrination children-are-born-atheist triumphalist cliches.