Why do SN2 reactions of alkyl halides proceed differently with KCN and AgCN?
The cyanide anion is an example of an ambident nucleophile, these are nucleophiles which, usually due to delocalisation, are able to attack an electrophile from two or more atoms. In the case of the cyanide anion reaction from carbon leads to nitriles, whilst reaction from the nitrogen leads to isocyanides. 

To give the full IUPAC definition from the Gold Book:


  A description applied to a chemical species whose molecular entities each possess two alternative and strongly interacting distinguishable reactive centres, to either of which a bond may be made in a reaction: the centres must be connected in such a way that reaction at either site stops or greatly retards subsequent attack at the second site. The term is most commonly applied to conjugated nucleophiles. [...]


To answer your actual question about why $\ce{KCN}$ forms nitriles, whilst $\ce{AgCN}$ forms isocyanides, March's Organic Chemistry (7th ed., p 449) gives a pretty concise answer in which we consider the extent to which the cyanide anion is associated with the counter-cation in solution. 


  All negatively charged nucleophiles must of course have a positive counterion. If this ion is $\ce{Ag+}$ (or some other ion that specifically helps in removing the leaving group), rather than the more usual $\ce{Na+}$ or $\ce{K+}$, then the transition state is more $\mathrm{S_N1}$ like. Therefore the use of $\ce{Ag+}$ promotes attack at the more electronegative atom. For example, alkyl halides treated with $\ce{NaCN}$ generally give mostly $\ce{RCN}$, but the use of $\ce{AgCN}$ increases the yield of isocyanides ($\ce{RNC}$).


                           [See footnote]


This, of course, is not a complete picture, as solvent effects greatly change the reactivity. In protic solvents (MeOH, to give a possible example) the most electronegative atoms (nitrogen in this case) will be solvated to a greater extent via H-bondng. In aprotic solvents (THF/DMF, to give possible examples) the anion isn't solvated quite so much (on either atom of the nucleophile), but the cation is somewhat more solvated, leaving the nucleophile more able to attack (via the most electronegative atom as this is the most nucleophilic in the absence of other factors). 



Footnote: As an aside at this point, it's important to realise that these are not fully covalent bonds (in $\ce{AgCN}$ and $\ce{KCN}$), despite having some level of covalent character. We're somewhat bastardising the Winstein model of nucleophilic substitution in which we define $\mathrm{S_N1}$/$\mathrm{S_N2}$ based upon the separation between the cation and the anion at the point when substitution takes place, with the two mechanisms being extremes of a continuum: