Lets take the favourite example of students ie. formation of NaCl
The Electronic configuration of Sodium (Na) is,
While the Chlorine ( Cl ) have Electronic configuration,
1s2,2s2,2p6,3s2,3p5
Each & Every element always try to complete its octet by keeping 8 electrons in its valence shell.
Sodium hates it own electron from its valence shell because there is only one electron in its valence shell (K=1). Sodium has already completed the octet in previous inner shell of valence shell (L=8). That is why Sodium don't want that 1 one electron situated in valence shell. Sodium is sad now. :(
On the other hand, Chlorine ( Cl ) has 7 electron in its valence shell so Chlorine (Cl) want only one Electron in its valence shell to become happy by completing its octet. Hence due to lack of 1 electron Chlorine is also upset. :(
That is why Chlorine steals that unwanted electron which was present in valence shell of Na.
Now, Outermost shell of Na ie. shell-M disappears because there is no any Electron. It leads to the Inner previous shell-L to be the outermost shell of Na. ie. Valence shell of Na which contains already 8 Electrons. So Now Na is happy! :)
Initially Chlorine had 7 electrons in its valence shell, Just now, Chlorine have stolen that 1 unwanted electron of sodium. So now Cl has 7+1=8 Electrons in its valence shell. It means octet is completed. So Cl is also happy now ! :)
Sodium (Na) lost its 1 electron, it means Na lost its 1 -ve charge ie. Na got 1 +ve charge. Hence Sodium (Na) become Cation.
Chlorine (Cl) steals the electron & hence it got 1 -ve charge ie. Chlorine (Cl) become Anion.
Both Na & Cl made happy each other, Na has 1 +ve charge while the Cl have 1 -ve charge. Since Equal & Opposite charges attract each other. So Sodium (Na) & Chlorine (Cl) Sticks together & forms NaCl, Because both made happy each other ! :)
Your question asks, "How to know the element whether it is metal or non-metal by using number of valence shell electron?" Right?
That is why I've given you this funny example, by using this example you can conclude that,