When the computer starts booting, the host name is invariably set to localhost (there is a line to that effect in the file /etc/rc.boot). In the configuration phase (still during boot) the host name is set by the script /System/Library/SystemConfiguration/Kicker.bundle/Contents/Resources/set-hostname called by the configuration daemon configd. Basically, what set-hostname does is the following:
1) If the variable $HOSTNAME is set to anything other than AUTOMATIC in /etc/hostconfig, then the host name is set to $HOSTNAME
2) If not (i.e. if the variable IS set to auto), then the hostname is set to (in this order):
a) Your (fully qualified) symbolical internet address (i.e., foo.homeip.net, for instance) if you have a static (i.e., permanent) IP number
b) Your local computer name, with .local appended to it, if this name has been set. (The local computer name is shown when you click the "Edit ..." button in the Sharing preferences pane. It can be set separately from the "Computer name" appearing in the main editable field in the Sharing pane, although by default it coincides with the "Computer name")
c) localhost
Moral: any manual changes to the host name will inevitably disappear after boot. If you don't like this setup, the easiest thing to do is to change
HOSTNAME=-AUTOMATIC-
in /etc/hostconfig to, for example,
HOSTNAME="cool.hostname.com"
Then your terminal prompt (at least in tcsh, which is the shell I use) will be the result of "hostname -s" (i.e. "cool" in the previous example).