im not an expert on this subject, far from it actually. but i would say that most of the time chan is for girls and kun for boys. since ive heard each used for the opposite id guess its really the speaker's preference. personally i stick to kun for males and chan for females to stay on the safe side.
-chan is used generally for really small children, although the usage is for little girls. Older men will refer to women by -chan when they are either being ironic, trying to be funny, trying to gain some help from a woman or win a favor from them, or being somewhat sleazy (all depends on the situation).
-chan will also be used for boys when women think the boy is cute, or they are gay. A guy who is gay will refer to another guy as -chan as well. Again, all depends on the situation and usage at the time...something you'll need to figure out by observation)
-kun is the suffix typically used for guys, or boys, but they are also used for tomboys, typically girls who are part of gangs or don't want to fit the ojousan type...
I could be wrong (feel free to correct me), but I also thought that in some cases, guys who are-say-childhood friends might add "chan" onto the end of their friend's names. I'm not quite sure if this if for teasing, or simple affection, but I'm almost sure I've heard it (albeit...in dramas *lol*! one source of 'information' isn't always the best for general application).
I usually use "chan" for my friends and younger friends/relatives.
I use it for both male and female. If I call my male friend with "chan", I think I consider him as my younger brother or close friend. "chan" sounds cute, too. .and usually first name+chan.
How to use "kun"..
first name+kun --- <male to male/ male to female> government use, very rare
<female to male>friendly and a little polite, for the young
<female to female>for fun or tomboy
last name+kun --- <male to male/ female to male> common for all ages, co-workers, classmates, friends etc..
<female to female>never or for fun
nickname+kun --- <male to male/ female to male>friendly and close relationship (ex. family or friends).. common for all ages.
<female to female> very rare