Disclaimer: This is mostly like canonical or "textbook" immunology. As with many biological systems, there are caveats to everything and it can't all be covered here. If you have questions outside the scope of this answer, please make another question!
The immune system consists of two subsystems: adaptive and innate immunity. Adaptive immunity as you know consists of T cells and B cells, and fronts much of the resistance to established infections. In fact, without adaptive immunity, you end up with a disease called SCID (severe-combined immunodeficiency), and HIV depletes CD4+ T cells resulting in AIDS. Important no doubt, but adaptive immunity has a process to it which makes it useless against brand new threats to which immunological memory doesn't yet exist. This is where the innate immune system comes in.
So we can either use the mucosal layer in the GI tract or the skin as an example, either way. Of course, the primary defense against foreign threats are such physical barriers as mucus, skin, the ciliary system of the lungs, and so forth. But lets say you get a cut or abrasion and now the skin barrier is compromised and a slew of bacteria enter the tissue. Your tissues contain resident cells such as macrophages (macs) and dendritic cells (DC) which have pattern recognition receptors that either recognize PAMPs or DAMPs. So what's a good PAMP? Lipopolysaccharide (LPS or endotoxin) comes to mind, and is recognized by toll-like receptors on DCs and macs, specifically TLR-4. These cells begin to produce mediators of inflammation such as IL-8 and IL-1ß which attract cells from the blood. The primary innate immune cell in your blood is a neutrophil, a type of phagocyte, which kill bacteria in a variety of ways. You also have a system of plasma (blood) proteins produced in the liver called complement which attempt to coat and directly kill the bacteria, or direct innate cells to targets of interest.
So say the bacteria is in the tissue, the tissue is inflamed, and a dendritic cell engulfs and digests a bacterium. The dendritic cell is what's referred to as an antigen presenting cell or APC, and APCs act to bridge innate and adaptive immune systems. Through an exogenous pathway sort of like this, they break down the bacterial proteins and "present" them on these MHC-II complexes (HLA in human). Macrophages, B cells and T cells may also be APCs. So when the dendritic cell is digesting the bacterial proteins and preparing to present them on MHC complexes, they move out of the tissues into the lymph system by expressing lymph node trafficking receptors such as CCR7 which responds to chemokines secreted by activated endothelium in the lymphatic vessels. The endothelium is activated by the inflammatory signals we talked about earlier. The DC goes to the lymph node where naive T cells and B cells aggregate, circulate in the blood, and re-aggregate.
Naive T cells tend to go back to the lymph node because they too express trafficking receptors such as CCR7 and CD62L that say "follow signals back to the lymph node." So this is where the magic happens. I'm not going to cover adaptive immunity but in short, typically APCs present antigens derived from foreign threats to naive T and B cells in the lymph node, thus activating them. These cells differentiate into a variety of lineages including effector and memory cells. As a note, you first adaptive response against a new threat takes about 2 weeks from start to finish to get antigens to an expanded pool of antigen-specific clones. Memory cells improve the second response by largely forgoing the wall of text above, proceeding directly to clonal expansion and elimination.
- Abul Abbas, Andrew H. Lichtman, Shiv Pillai (2014). Cellular and Molecular Immunology (8th ed.). Elsevier. ISBN: 9780323315906.