What is a JTAG?
It is like USB, SPI, I2C, and other "busses", and it has a number of popular uses, not limited to:
One in particular is in testing of silicon before too much is invested in each part, for example while a chip is still on the wafer you can check most of the part. Granted dicing the wafer can do damage so you want to test again but maybe you do that before packing, maybe after.
You can use it to do boundary scan on boards. You can take a board on a production line (granted the board has to be designed right and some percentage of the chips have to support this) but you can do low speed connectivity tests, stimulate the pin on one end of a trace and scan other parts to see that they are or are not connected per the design of the board.
Since the chips already have these dedicated pins why not, for processors, use that same interface as a way to talk to an on chip debugger (OCD), design something into the processor and allow that to be talked to via jtag.
It is a generic-ish way to allow you to isolate things on the chain you want to send a series of bits to or get a series of bits from, in a way that each thing you want to talk to can be designed for various numbers of bits from a small number to a large number. For a debugger you would naturally have it write to or read from a register-sized thing in your chip - maybe a 16 bit register or 32 bit. But for silicon or board testing your scan chain might be dozens of bits. Each individual thing you address can vary in size, if you want, from any other thing, making this a very versatile bus with a relatively small number of pins that is attractive for these types of use cases.
Perhaps because of the popularity for software debugging they now have some two pin solutions to save on pin count for microcontrollers and perhaps others will adopt that, perhaps not.
JTAG was originally an interface used for testing PCBs after assembly. The IO lines on the chips could be controlled and read via the JTAG ports allowing a board test sequence to be performed. Later on it also started to be used as a programming and debug interface. What functionality is available over JTAG will depend on the particular IC you are dealing with.
One advantage of JTAG over most other programming interfaces is that devices can be daisy chained. So one JTAG port can serve to program/debug/test multiple devices (though I would advise designing your systems so that different brands of chip can be split on to different chains, while the low level JTAG protocol is pretty standardised unfortunately the tools you need to program/debug the chips are not).
JTAG is somewhat an odd term. To begin, the term can specify a specific connector and protocol, composed of a clock signal (TCK), mode-select (TMS), and data in/out (TDI/TDO). This comprises a network of devices, where each device's TDO is connected to the next device's TDI in a so-called scan chain. TMS is used to put all devices simultaneously into various modes.
The devices on the scan chain are typically connected as one large shift register comprised of registers in each individual device. When devices are in the SHIFT_IR or SHIFT_DR state, they connect to the scan chain one of two temporary registers: An instruction register, and a data register.
Once a value is shifted in, the UPDATE_IR or UPDATE_DR mode can be activated by setting TMS appropriately while toggling TCK. At this point, the value shifted into the register "takes effect". For example, shifting various sequences into the IR of an FPGA and transitioning into UPDATE_IR will cause the FPGA to enter various modes such as configuration, readback, normal runtime, or access specific user registers and JTAG-specific primitives instantiated in the design.
Likewise, capture states can be used to "read" a value from the device and put it into the IR or DR, to be shifted out through TDO and ultimately read by the JTAG programmer.