A pipe or redirection won't remove any colors!
The initial command that you're using removes the colors when it notices that the output is not a terminal! So cannot be a general answer to that question.
See e.g. man ls:
ls emits color codes only when standard output is connected to a terminal.
You can change the behavior using --color=always.
Same for grep:
--color[=WHEN], --colour[=WHEN]
Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields
and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment
variable GREP_COLORS. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority. WHEN is
never, always, or auto.
For example:
echo 123 | grep --color=always 2 | tee file
... will have the 2 colored for the output in the terminal and also in the file.
To have the colors in the output, but not in the file, you can check answers to this question and then run e.g. this:
echo 123 | grep --color=always 2 | tee /dev/tty | ansi2txt > file