Why did the cyanide ion attack the carbon atom?

The transition state is a 5-center structure with serious crowding about the carbon atom.

Note that the carbon atom has undergone an inversion during this process.

The potassium ion remained a spectator ion throughout this reaction.