Note that comma usage was a little different when that was written. The modern version would omit the comma after "militia" and the one after "arms":
A well-regulated militia being necessary to the security of a free state, the right of the people to keep and bear arms shall not be infringed.
The italicized part is an "absolute phrase/construction", which is independent of the structure of the rest of the sentence. In this example, it establishes a reason for the clause that follows it.
Wikipedia "absolute construction"
There are a few other examples at that link of an absolute construction in English; they all use participial verb forms.
The sentence could be rephrased like this:
The right of the people to keep and bear arms shall not be infringed because a well-regulated militia is necessary to the security of a free state.
The verb "to be", which appears as "is" in this rephrasing, occurs in participle form "being" in the absolute construction.