There are 3 ways to enable 24-bit color in emacs:
(code is from emacs's term.c)
(Note: a simple way to test if colors are working is by checking M-x list-colors-display)
The correct method is to set TERM to a value which supports direct color (via the RGB terminfo capability), if one is available for your terminal (or just try xterm-direct).
These are generally named "(terminalName)-direct" (xterm-direct, vte-direct, etc.)
ex: TERM=xterm-direct emacs, or set TERM properly by configuring your terminal.
Edit:
There is a flaw in the implementation of xterm-direct (and related). Certain shades of blue are treated as indexed colors (due to it using the same sequences for rgb and indexed color), and won't render correctly. I recommend using the second method.
/* Standard support for 24-bit colors.  */
else if (tigetflag ("RGB") > 0)
  {
    /* ...  */
    tty->TN_max_colors = 16777216;
  }
Another option is to use a terminfo file with the nonstandard terminfo capabilities setf24 and setb24 (see answer https://stackoverflow.com/a/50577683/6232794). It's possible that this method is supported in older versions of emacs than the other two
const char *fg = tigetstr ("setf24");
const char *bg = tigetstr ("setb24");
/* Non-standard support for 24-bit colors. */
if (fg && bg
    && fg != (char *) (intptr_t) -1
    && bg != (char *) (intptr_t) -1)
  {
    tty->TS_set_foreground = fg;
    tty->TS_set_background = bg;
    tty->TN_max_colors = 16777216;
  }
As a last resort hack, you can set the environment variable COLORTERM to "truecolor", which might work with your terminal.
/* Fall back to xterm+direct (semicolon version) if requested
   by the COLORTERM environment variable.  */
else if ((bg = getenv("COLORTERM")) != NULL
         && strcasecmp(bg, "truecolor") == 0)
  {
    tty->TS_set_foreground = "\033[%?%p1%{8}%<%t3%p1%d%e38;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%d%;m";
    tty->TS_set_background = "\033[%?%p1%{8}%<%t4%p1%d%e48;2;%p1%{65536}%/%d;%p1%{256}%/%{255}%&%d;%p1%{255}%&%d%;m";
    tty->TN_max_colors = 16777216;
  }