Valency is related to the number of electrons in the outermost shell and orbitals in an atom of the element.
if the total number in the outer most shell (K, L, M,..) are 1, 2, 3, 4 then we say valency is 1, 2, 3, 4 respectively. group number.
if the valence electrons are 5, 6, 7 , 8 then valency is 3, 2, 1, 0 respectively. The last one is for the noble gases. The number starts from 3 because these are the electrons the atom will try to get from another atom.
This above rule works for atoms with orbital filling up to 4s^2 4p^6.