Even before the time of Jesus, many mystery religions of the ancient Mediterranean had some sort of ceremony that people had to do in order to join the cult. Some of these, like the Eleusinian mysteries, involved dipping yourself in water to wash away your past life, and to show that you were beginning a new life.
Christianity, from the very beginning, also had this idea of dipping yourself in water, which we call baptism. Before Jesus began preaching, his cousin, John dipped Jesus in water to baptize him. That’s why we call Jesus’ cousin John the Baptist.
During the Roman Empire, Christians, like other people, were usually baptized as adults. That way they were old enough to understand what they were doing. In fact, people often waited to be baptized until they were dying. Being baptized was supposed to wash away all your sins, so you could go to Heaven. Of course people wanted to wait until the last minute so that they would be sure of going to Heaven. If you got baptized earlier, you might do something bad, and not go to Heaven after all. The emperor Constantine, for instance, was not baptized until he was dying, in 337 AD.
But people worried about dying by accident, quickly, without having time to get baptized. So starting in the 200s AD, Christian bishops came up with another answer. Instead of waiting to be baptized, you could go ahead. Then if you did anything bad later, you could say you were sorry and do some sort of penance, like eating only bread and water for a while. That would make it okay again. So by the Middle Ages, people started baptizing their babies, to be sure of getting it done in case the babies died.