Generally, the natural parents will have a presumptive right to custody. Only in cases where the parents are found to be unfit, or there are exceptional circumstances, will third parties be granted custody. At any time after a divorce, grandparents may petition the court for visitation rights. Grandparents have the right to petition the court for visitation rights in Massachusetts. Recently, the U.S. Supreme Court has held that a parent's proposed visitation schedule, unless it denies visitation entirely, is presumptively better than a grandparent's proposed visitation schedule.
Also see AARP Grandparent Information Center.