Why does one color name have different hex codes?
Because color names are not a standard; they are lists. The CSS list descends from the X11 window system file of the 1980s, the xkcd list comes from a 2010 survey in which people named colors on screen, and community lists collect names from many places.
So lavender is a near-white tint in CSS but a clear light purple in the survey, and navy in the survey is darker than the CSS keyword. Both are honest records of different things: one is a technical definition, the other is how people use the word.
This site shows every hex code a color list gives for a name, with its source and how different the codes look, measured in CIEDE2000. Under about 2 the difference is hard to see; above 10 the values look like different colors.
Which hex code should I use for a color name?
For code, use the CSS value, because browsers understand the keyword. For describing a color to people, the survey value is often closer to what they imagine.