While some people are religious at least in part because they desperately want to believe in an afterlife that their religion promises them, not all religious people believe in an afterlife and not all those who believe in an afterlife are as blithely optimistic as the average contemporary American that of course they themselves will spend it in heaven. There are many people who not only adamantly persist in believing in fantastic, unsupportable, superstitious, supernaturalistic afterlife nonsense but actually do so despite having increased anxieties about death on account of precisely those beliefs. Not everyone believes the implausible because it makes them happier to do so.
And do you know what religion’s adherents have a particularly high anxiety about death and what the reasons for that anxiety are? Read Tom Rhees’s discussion of some research into this for some possible answers.