All color consists of certain proportions of red, green and blue light—including that emitted by the computer. To tell the monitor which proportions of these colors for a given pixel, one assigns a RGB color value for the intensity of red, green and blue respectively between 0 and 255.
This is then converted into a hex triplet value, that is, a six digit, three byte sequence that consists of each of the three RGB values. Hexidecimal is in base 16, as opposed to our usual decimal base 10, so a conversion process is necessary.
To manually convert from the RGB value to the hex triplet, take that RGB value and divide by 16. The integer in the number represents the first digit: 0-9 is represented by the same numbers, while 10-15 are represented by A-F. The remainder provides the second hexadecimal digit in the same fashion.
An abbreviated form of the hex digit may also be used if each byte consists of a double digit. For instance, 1188FF could be shortened to 18F. However, this is primarily used for CSS, not HTML, so it is not recommended.
With this system, any color is possible: there are almost 17 million unique colors represented by this system, so there's no shortage of flexibility!