Look at it this way: The atom needs to have a complete octet in its outermost shell, and will give away/accept electrons to achieve this goal. Sodium, for example, can give away one electron (+1 valency) or accept 7 (-7). Generally accepting more than 4 electrons is hard, so you don't see the latter happening. Similarly, oxygen has $2,8,6$ -- it can accept 2 electrons (-2) or give up 6 (+6, never observed)
Note that the octet rule is not universally applicablethe octet rule is not universally applicable. Elements can have multiple valencies, not all satisfying the octet rule. To understand this you need to understand orbital theory first (which will be hard to explain in the scope of one post).