Why is it legal for some states to regulate the buying and carrying of guns?

Good question- without a really good answer. The 2nd amendment to the US Constitution provides that the "right to bear arms" "shall not be infringed". However, this issue has only partly been ruled on by the US Supreme Court. Thus both State and Federal laws that DO infringe on the right to bear arms have remained in force. The Court has ruled that a blanket prohibition against possession of firearms is NOT legal, but other laws (several thousand of them) remain in force.