They allow you to more easily see which tag and note changes go to the same post (since they all get interleaved otherwise). The colors vary with the age of the post.
Posted under General
It sort of cycles whilst slightly changing each cycle. The actual formula involves chopping the ID up into pieces, and then using each piece to generate a channel of the color.
The whole ID is used to cycle through 256 values for the red channel. Post 1 has red value 1, and it increases until post 255 which has red value 255, it then repeats from 0 back up to 255, and so on.
For every multiple of 256 in the ID, the green channel cycles from 0 to 255.
For every multiple of 65,536 (256²) in the ID, the blue channel cycles from 0 to 255.
Essentially the color is representing a three digit, base-256 conversion of the post's ID.
Because the post ID's on Danbooru have only gone up to about a million or so far, the blue channel only reaches 15 or so (out of 255) which is why the colors mostly just vary between red and green, depending on how close a post's ID is to a multiple of 256 or 65,536. Black means you are pretty close to the start of a new cycle of red (and green).
The way this works every post up to roughly 16.7 billion will have a unique color (even though each cycle will be imperceptibly similar to the previous one), before the whole thing overflows and cycles again.
To get an idea of how this works, see this page I posted that generates colors in the same way.
Updated