Over at the Google G+ Emacs Community, Agu Monkey has a pointer to an excellent tutorial on writing Emacs minor modes by Christopher Wellons. Most of us probably have no interest in writing a full-fledged minor mode (although Wellons' tutorial is complete enough for that) but it's often an easy way to provide some custom key bindings for a major mode or otherwise provide additional functionality.
Wellons takes us through the development of a trivial minor mode that merely inserts the word “foo” when called with a 【Ctrl+c f】 key sequence and keeps a count of how many foos are inserted. He shows how to make a buffer-local variable to count the insertions, how to instantiate a hook-function variable that users can insert hook functions into to run their own code when the minor mode is toggled, how to define a keymap for the minor mode, and how to activate the minor mode when a major mode is activated.
This is a really useful tutorial that you should read and then bookmark for the day that you want to add a little extra functionality to some mode or when you want to write a full blown minor mode. Highly recommended.