My question is, why aren't drivers for a particular device stored on
the device itself?
This would mean the device would need to have storage and the operating system would have to access it. There are security concerns with storage devices in some cases. It overall is a bad solution to a problem that can be solved by simply downloading the driver itself. A USB device can indicate to the OS it is ANY TYPE OF DEVICE, which is the reason, there are security policies to only allow certain types of USB devices and to block anything device that isn't recognized.
For example, lets say that I have plugged in a keyboard to my
computer. Is it possible for drivers for particular versions of
Windows, Linux and OS X stored on the device itself?
In this specific example the generic keyboard I/O driver would work.
So that when plugged in the first time, the device will interact with
the OS and install the appropriate driver?
This already happens to some degree. You would have to still install the driver even if it was stored on the device itself.
Does it have to do with the fact that there are many OSes and many
versions of these OSes and hence, the device would need to store a
very high amount of the same purpose code for different platforms?
A device either has drivers for a particular OS that exist or those drivers don't exist. If they are written for the OS they can are easily found and installed. Since device drivers are also updated often, the memory containing the driver, would almost never contain the current version anyways. Which is exactly what happens with the driver disks. It is extremely rare to have a computer that is not connected to the internet unless its by choice. If its by choice there often is a device, that can be used, to transfer data to it.
I guess most OSes now have common drivers packed with them but if our
device needs a driver not already in the OS, then we might have a
problem right?
So you download the driver from the manufacture and place it on a media that can be transferred to the machine. If this still isn't possible then in 99.99% of the cases a device that does require a driver to be used, will itself come with media from the manufacture, and that media is cheaper then any alternative. If that still isn't possible then the machine should be configured ahead of time so the device can be used.