the octet rule states that elements prefer to be surrounded by 8 electrons. there are certainly elements that don't obey the rule, but many of the main group elements ( not transition metals) do.
the number of valence electrons can be determined usually from the group the elements are in on the periodic table. The noble gases have 8 (Group VIII), halogens have 7 (Group VII), oxygen selenium and sulfur have 6 (Group VI), N and P have 5 (Group V), C has 4 (Group IV), etc. The noble gases have a full octet (four lone pairs), so they are inert. Most other elements have to combine with other atoms to achieve an octet of 8 electrons.
Start by adding the valence electrons to the element one at a time until it has four around it. If you're working with, say N, there will be 5 V.E. Once you add 4 single electrons around N, you have one more left over and this one will combine with one of the single electrons to give a lone pair. So you have a N with one lone pair and three single electrons around it. In order to achieve the octet rule, you need 3 more electrons, which can be gained by combining each of the single electrons with something else, say H atom (has one valence electron b/c Group I). When you add the H atom to the three single electrons on N, you get NH3. So, the N has one lone pair (2 electrons) and 3 covalent bonds (at 2 electrons each) for a total of 8 electrons around N.