In the context and vernacular of the Eighteenth Century, "well-regulated" meant "well-organized, well-trained and well-equipped".
If you read the supporting discussions in the Federalist Papers, controlling a standing army was discussed at length while discussing whether there should even be a Federal standing Army.
The conclusion was that a small standing army would be overwhelmed by the sheer force of armed citizens with or without martial training, and thus kept in line.
The term "Militia" is defined in the same dictionary as: "the Army, in its entirety."
When the substitutions are made, (remembering the objective is the "security of a free State"), keeping the army controlled by guaranteeing the Right of the People to Keep and Bear Arms, and thus keeping the Federal Government in check was indeed the objective of the Second Amendment, to ensure liberty.
The Third Amendment dealt with another related concern: that of quartering troops within one's home.
Recall, too, that the colonies had been subjected to what amounted to military rule prior to the fight for independence, and the pieces all fall into place.