Valency is a concept that describes the power of an element to bind to other elements. There is an official definition in the IUPAC goldbook:
valence
The maximum number of univalent atoms (originally hydrogen or chlorine atoms) that may combine with an atom of the element under consideration, or with a fragment, or for which an atom of this element can be substituted.
So in principle it is not possible to know the valency just from the electron configuration, but you have to also know the chemistry it performs. It is also noteworthy, that according to this definition all elements have only one value for it's valency.
In the first period, according to the definition, hydrogen is monovalent, i.e. $\ce{H2}$ or $\ce{HCl}$. Helium on the other hand is technically divalent, since $\ce{HHeF}$ was theoretically predicted, see wikipedia. But it is somewhat safe to assume helium is inert and therefore zerovalent.
In the second period, where the octet rule holds, it is the number of unpaired electrons in the valence shell, with the exception of carbon. Unfortunately this is also the period which behaves most different from the rest of the periodic table. So the best way here is to just learn them as exceptions: $\ce{NaCl}$ (mono-), $\ce{BeCl2}$ (di-), $\ce{BCl3}$ (tri-), $\ce{CCl4}$ (tetra-), $\ce{NCl3}$ (tri-),(1) $\ce{Cl2O}$ or $\ce{H2O}$ (di-), $\ce{FCl}$ or $\ce{F2}$, $\ce{Ne}$ is zerovalent.
Below the second row it becomes a little bit more predictable. For most main group elements it is the number of electrons in the valence shell. This obviously coincides with the highest stable oxidation state.
Some examples may illustrate that. For the first group all binary compositions of the alkaline element $\ce{A}$ of the composition $\ce{ACl}$ are known. This is also the maximum of chlorine atoms any of these elements can bear, hence they are all monovalent.
The same applies to the earth-alkalines, $\ce{Ea}$. Here all binary chlorides of the form $\ce{EaCl2}$ are known, hence they are divalent.
For the triels $\ce{M^{(III)}}$, all of the chlorides of the form $\ce{M^{(III)}Cl3}$ are known. However not all of them are very stable, i.e. $\ce{TlCl3}$ (wikipedia). So they are trivalent.
For the tetrels, $\ce{M^{(IV)}}$, the trend continues. The binary chlorides $\ce{M^{(IV)}Cl4}$ are known, even $\ce{PbCl4}$ is quite stable below $0~^\circ\mathrm{C}$ (wikipedia). For Flerovium only predictions have been made, but it's believed to be very unstable in its tetravalent state due to the inert pair effect.
For the pnictogens, $\ce{M^{(V)}}$, all binary chlorides up to and including antimony are known. However, also $\ce{BiF5}$ is known, hence all elements can be considered pentavalent.
For the chalcogens, $\ce{M^{(VI)}}$, all binary fluorides with coordination number six are known. Most prominent is sulfur hexafluoride, for which a chloride counterpart exists. The elements can therefore be considered hexavalent. The tetra chlorides are also stable for all elements of this group, in that case the elements can be considered tetravalent. However, more common are compounds where the chalcogens are divalent, i.e. in the respective hydrogen compounds, $\ce{H2M^{(VI)}}$.
The halogens $\ce{Hal}$ are just amazing, and once you have looked into interhalogen compounds you will most likely remember a lot of them. For chlorine, the interhalogens $\ce{ClF_{\{1,3,5\}}}$ have been reported, making it pentavalent. For bromine, the same holds true: $\ce{BrF_{\{1,3,5\}}}$. Only $\ce{BrCl}$ has been observed. However this makes bromine also pentavalent. For iodine the colourless gas $\ce{IF7}$ has been observed, making it the only heptavalent element of the series. For astatine only a few compounds are known, and the current believe is, that it is monovalent.
For the noble gases, $\ce{Ng}$, there are a few compounds known, and their valency increases throughout the group. While argon can be considered at most divalent due to the discovery of the highly unstable $\ce{HArF}$, the divalent nature of Krypton is undoubtedly proven by $\ce{KrF2}$. Xenon is the most reactive and potent noble gas and it can be considered hexavalent according to the above definition, as of $\ce{XeF6}$ does exist. However, also $\ce{XeO4}$ is known, which would make it octavalent according to a different definition.(2)
Transition metals are a whole different story and it is difficult to find a generalised rule or observation for it, since the IUPAC definition is very strict on the matter. All elements have to be looked at in detail. It will certainly also exceed the purpose of this answer.
Just to illustrate the problem have a look at manganese. There are various compounds known, such as $\ce{MnCl2}$ (divalent), $\ce{MnF3}$ (trivalent) and even $\ce{MnF4}$ (tetravalent). Binary hydrides have not yet been reported. However, there are oxides of various composition, ranging from divalent to heptavalent.
Notes
- The molecule $\ce{NCl5}$, pentavalent, has been theoretically suggested, see wikipedia and references within.
A more modern interpretation of the valency concept allows elements to have more than only one value. It is therefore always depending on the compound in which it is looked at. A very brief summary can be found on wikipedia.
The most characteristic chemical property of an element is its valency. There are numerous measures of valency each with its own area of usefulness and applicability. Simple definitions refer to the number of hydrogen atoms that can combine with an element in a binary hydride or to twice the number of oxygen atoms combining with an element in its oxide(s).
(N. N. Greenwood and A Earnshaw: Chemistry of the Elements. Second edition. Butterworth-Heinemann, 1997. Chapter 2, p. 27.)