To explain this question let us use an example of reacting Bromoethane (CH3CH2Br) with ammonia (NH3). As bromine is more electronegative than carbon (electronegativity being the ability of an element to attract a pair of electrons) the C-Br bond is polar, ie the electrons are more attracted to the Bromine than Carbon. This means that the carbon in the bond possesses a slight positive charge which means that it attracts the lone pair of negative electrons on the Nitrogen in ammonia. Therefore a nucleophilic substitution reaction occurs forming CH3CH2NH2 and HBr.
Now there is still Bromoethane in the reaction solution and therefore it can either react with ammonia or the newly formed ethylamines (CH3CH2NH2). It will preferentially react with ethylamine rather than ammonia because of the alkyl group attached to ethylamine's nitrogen. Alkyl groups are electron releasing (due to the inductive effect). This is because Nitrogen is more electronegative than carbon, so the alkyl group's electrons shift along to the nitrogen, meaning that the lone electron pair on the nitrogen is more accessible as there is a greater density of negative charge now as opposed to the ammonia molecule’s nitrogen which doesn't possess any alkyl groups. Therefore as the nitrogen's lone pair of electrons in ethylamine is more accessible than the ammonia's bromoethane will react with ethylamine to form diethylamine. In diethylamine there are now two alkyl groups so the lone pair of electrons on the nitrogen is now even more readily accessible to the Bromoethane hence why the bromoethane will preferentially react with diethylamine to form a quaternary ammonium salt rather than reacting with just ammonia which would just form amines