The founders of a new colony, whatever Utopia of human virtue and happiness they might originally project, have invariably recognized that among their earliest practical necessities to allot a portion of the virgin soil as a cemetery, and another portion as the site of a prison.
Hawthorne suggests that whatever Utopia is established, there always will be death, and there always will be crime, that this is just part of humans living in a community. This isn't just applicable to Puritan New England, but to any new colony at any time in history. However, the Puritan settlers established a theocracy, and the prison was for moral as well as civil infractions, both being the same in their eyes. Similarly, the cemetery serves as an extension of the prison, incarcerating those who challenged or broke civil/moral law in the extreme. These two institutions, prison and cemetary, are tightly related in a theocracy, and in this case, express the rigid Puritannical philosophy of crime and punishment. Later in this chapter he mentions Anne Hutchinson, who was hanged for expressing her own belief about God, a belief which opposed the moral/civil authorities, who condemned her to death. She, like Hester, was a resident in the same prison, but Hester fortunately escapes hanging, although the Puritan matrons consider it an appropriate punishment for her crime.
I think it's to set the story in a dark town, and to show that it is a corrpupted town. Since a prison is considered evil, since Hawthorne is setting the story with these dark elements, he sets the story to a dark tone.