TERM is an environment variable, you set it in your environment. This could be the environment of a particular shell that you've opened, by running
export TERM=foo, or all of your shells by adding the same command to your .bashrc (or a similar file for your shell, if you don't use bash. but you probably do use bash).
termcap are databases containing information about terminals. A terminal was originally a hardware device that combined a display and a keyboard. Your computer would send commands to your terminal to tell it what to display, and of course the terminal would send most keyboard input to the computer (it would handle some keyboard input itself). These days your terminal is a software program running on the computer, but programs still talk to the terminal as if it were an external piece of hardware. The value you set this variable to depends on what program you're running. If you're running XTerm, then set it to "xterm". If you're running RXVT, then set it to "rxvt". And so on.
Anyway, the terminals were all designed and built by different companies who were all in competition with each other, and so they were all adding features as quickly as possible in order to out-do each other. Naturally, none of these companies designed their terminals using the same command set as their competitors; the sort of standardization we have with HTML and CSS today didn't exist back then. The
termcap database was created to let you use any terminal you happened to want with your computer. A program could query the
termcap database to find out what commands to use for your terminal. Ultimately the program wouldn't need to know anything about any particular terminal; it would just ask
termcap how to do everything.
terminfo is a newer database than
termcap. It contains essentially the same information, and can be considered interchangeable with
Programs that use
terminfo look at the
TERM environment variable to know what kind of terminal you're using; they pass the value along to the database when they make their queries.
eshell modes are not really considered terminal emulators; they don't provide any capabilities for changing the text color or moving the cursor around.
ansi-term, on the other hand, provide essentially all the features you would expect from a mid-range terminal such as a VT100, or xterm. I don't actually know off-hand what
TERM should be set to for these, but I think they set it for you.