Anyone who has an important role in a child's life may ask the court for custody. You don't have to be the child's parent.
When a Judge decides custody between a parent and someone who is not a parent, he or she will consider if there are "extraordinary circumstances". If there are extraordinary circumstances, then the Judge will consider what is in the best interest of the child.
Some examples of extraordinary circumstances are a parent's neglect or abuse of the child, abandonment of the child, a parent who is in prison, or if there are other serious acts which may affect the child's well-being.