I mean, why IP addresses are not sufficient? We can identify devices by their IP addresses. Why do we want to use hostnames? To identify devices?
I know that IP addresses are assigned in a random way (from a certain range). What about the host names? How are they assigned?
On all the zeroconf devices I've used the hostname is actually set on the device. On a Mac this comes from the computer name you set for your system. On my Linux and OpenSolaris machines it's the system hostname.
An IP address doesn't tell you anything about the device. As well as machines setting host names as Kamil explained, printers and scanners and cameras and storage devices can all have helpful names built in. So you might see names like "Inkjet Printer Model 1234" and "Digital Camera XYZ". Deciding which one will print your document is much easier than trying to choose between 192.168.1.11 and 192.168.1.12!
If you have more than one of a device, you can very often change the names, but for something like a printer, the name that's set at the factory is usually good enough. So to answer the second part of your question, for many devices, the names are assigned by being burned into ROM at the factory.