Step-by-step explanation:
This can be very well explained with the help of reactivity.
Reactivity of an element is defined as the tendency to loose or gain electrons.
Sodium is an element belonging to group 1 and is considered as a metal. This element can easily loose electrons and hence is considered as reactive element. This element tends to form positive ions.
Chlorine is an element belonging to Group 17 and is considered as a non-metal. This element can easily gain electrons and hence is considered as a reactive element. This element tends to form negative ions.
Carbon and nitrogen belong to group 14 and group 15 respectively. They are not very reactive because they can't loose or gain electrons easily. Carbon tends to form covalent bonds by sharing of electrons. Nitrogen very rarely forms ionic compounds.
This is the reason why carbon and nitrogen does not form ions but sodium and chlorine forms ions.