The elements in the Periodic Table are arranged in order of increasing atomic number. The atomic number is the number of protons in the nucleus. In a neutral atom the number of electrons is the same as the number of protons.
Electrons occupy shells. The first shell holds up to 2 electrons and the second and third shells hold up to 8 each. Fill the shells in order. Chlorine has atomic number 17, so it has 17 electrons and its arrangement is 2,8,7. Magnesium has atomic number 12, so its arrangement is 2,8,2.
This arrangement gives the position in the table. The group number is the number of outer electrons, so chlorine, with 7 outer electrons, is in Group 7. The period number is the number of occupied shells. Chlorine and magnesium both have three occupied shells, so both are in Period 3. Elements in the same group have the same number of outer electrons, which is why they react in similar ways.