Let's start with ammonia (NH3) A nitrogen atom has five valence electrons, and it wants a total of eight. It bonds with three hydrogen atoms, and "shares" one of its electrons with each hydrogen atom. The nitrogen atom now has six shared electrons and two unshared electrons, meaning the nitrogen atom now has eight. Hydrogen, on the other hand is special, as it only really wants two electrons in its outer shell, so it has a total of 2 shared electrons, and the octet or "Noble gas" rule is fulfilled, as hydrogen would then be like the nearest noble gas, helium.
For CF4, the explanation is much more simple. Each fluorine has seven outer electrons, so it wants to gain one more. Carbon has four electrons, and it wants to gain four. So, carbon shares one of each of its electrons with one of each fluorine's, making the carbon have eight electrons, and each fluorine has eight as well.