Which codes a text understands depends on the Minecraft version and on the plugin that shows it. When in doubt, check that plugin’s documentation.
The 16 classic colour codes
Every classic colour has a code: the section sign § followed by a digit or a letter. Most plugins on Java servers also read & instead of §, because § is hard to type.
&0 black, &1 dark blue, &2 dark green, &3 dark aqua, &4 dark red, &5 dark purple, &6 gold, &7 grey
&8 dark grey, &9 blue, &a green, &b aqua, &c red, &d light purple, &e yellow, &f white
The § codes work in Java and Bedrock Edition.
Bold, italic and the other formatting codes
&l bold, &o italic, &n underlined, &m struck through, &k obfuscated (letters that keep changing), &r back to normal.
In Java Edition a colour code ends the formatting before it, so write the colour first and the formatting after it: &6&l for bold gold.
Hex colours: any colour since Java 1.16
Since Java Edition 1.16 a text can have any colour, written as a hex code like #FFA915. Plugins write it in different ways:
&#FFA915, read by the TAB plugin and many others
{#FFA915}, used by some plugins
&x&F&F&A&9&1&5 or §x§F§F§A§9§1§5, the long form Spigot reads
<#FFA915> in MiniMessage, the text format many plugins on Paper use
Bedrock Edition and Java versions before 1.16 show no hex colours.
How a text gradient works
A gradient gives every letter its own hex colour, blending from one colour into the next. A gradient over ten letters is therefore ten colour codes, and the text gets long; some places limit its length.
In MiniMessage a gradient can also be one tag, <gradient:#FFA915:#FF7A05>, if the plugin reads MiniMessage.
At SMPHUB: The free Gradient maker on SMPHUB writes these codes for you, in every format above or one of your own, with a live preview in the game’s letters. For plugins that read MiniMessage, the MiniMessage Gradient writes the one <gradient> tag, with phase and rainbow.
Where colour codes work
Chat, player names, the tab list, scoreboards, holograms, signs, item names and the server list entry (MOTD) are each shown by the game or by a plugin, and each reads codes its own way.
A plugin that reads MiniMessage does not read &#RRGGBB unless it says so, and the other way round. Look up the format in the plugin’s documentation and pick the same one in the Gradient maker.
Small caps and other letters
Where you cannot change the font, Unicode letters help: small capitals like ꜱᴍᴀʟʟ ᴄᴀᴘꜱ look like a font of their own in Java Edition chat, in item names and on Discord. They are characters, not colours, so they work together with any colour code.
Unicode has no small capital x, so x stays x.
At SMPHUB: The free Small Caps generator on SMPHUB turns your text into small capitals and two more styles, ready to copy.