Both are correct.
Absence of the article "the" indicates that you are not talking about any particular afterlife.
Presence of the article "the" indicates that you are talking about a particular afterlife that you have already mentioned previously.
(starting conversation) Hello, do you believe in an afterlife? -- You have never defined an afterlife yet, hence no article.
(or) The Bible talks about life after death where good guys go to heaven and bad ones go to hell. Do you believe in the afterlife? -- This suggests that you are talking about about this particular afterlife, not some totally different (for example Viking) afterlife.
It is worth noting that article "the" relates to "this". In a way, when you say "the object" you are saying "this object (that I have mentioned before)". Also, "a" / "an" relates to "any", thus saying "an object" means "any object (pick one to your liking)".