How did the Commodore 64 keyboard work?
Does the C64 constantly poll the keyboard? 


My experience is with the TRS-80, but yes, the main processor typically did the keyboard scan.  No such thing as a secondary processor -- remember that the first 8-bit microprocessors hit the market priced at several hundred dollars for a device in a 40-pin DIP.  Even when the price dropped, the price of the CPU was a pretty big fraction of the computer's price.


  How would the C64 have enough time to do other things such as running programs, if it's spending so much time polling the keyboard? (and also drawing the screen, and polling joysticks, for that matter?)


By doing everything very slowly, by today's standard.


  Is it all down to the kernel? Does the kernel say something like "poll the keyboard, then poll the joysticks, then run a couple instructions of a program, then repeat"?


Either that, or the keyboard scan only happened when the user program asked for one.

"kernal" is actually a pretty grandiose term to apply to the operating systems of the day.  There wasn't a concept of an OS that would share time between applications.  Even DOS running in an IBM PC didn't have a 'kernel' per se -- at best it had a few TSRs running in the background, but there was nothing like a concept of having two user-space programs running simultaneously.  Basically, whatever you were running owned the entire machine, and the only thing the OS (such as it was) did was take over for scanning the keyboard or accessing disks (if you had them) or whatever.