K-9 was the inspiration of writers Bob Baker and Dave Martin. Graham Williams was quickly sold on the idea of a robot dog and ended up using him in many subsequent stories. (If you count all the stories from K-9's arrival to his departure, he is the longest-lasting Companion of all. Of course, there were numerous stories in which he did not appear, so this form of reckoning is somewhat unfair.)
K-9 Mark I was Professor Marius' data-analyzer computer, built in the form of a dog in memory of the Professor's own dog, who could not be brought along to the Centre for Alien Biomorphology on asteroid K4067 due to weight restrictions. This K-9 developed a fondness for Leela and traveled with her and the Doctor until Leela left the TARDIS to stay on Gallifrey.
K-9 Mark II was built by the Doctor from a kit of unknown origin (perhaps the Robot-Dog-of-the-Month-Club). He became associated with Romana, and departed the TARDIS when she did.
K-9 Mark III was also built by the Doctor, though, interestingly, his hardware is sufficiently simple to be understood by the 20th-century Brendan. This K-9 was left as a present for former Companion Sarah Jane Smith, and accompanied her in the adventure documented in K9 and Company. He shows up again in "The Five Doctors," but this time gets left behind.
There is also a K-9 in the "Dimensions in Time" skit, perhaps Mark I since Leela is also around at about the same time.
Back to the TARDIS.