How does the immune system recognize harmful entities?

Adaptive immune system activation relies on antigen presentation. Antigens are immunogenic molecules expressed by pathogens, tumor cells, or other “foreign” entities. Antigen-presenting cells (APCs) such as macrophages or dendritic cells sequester these molecules and expose them to adaptive immune system effector cells such as T cells and B cells, resulting in the selective targeting of cells expressing that specific antigen by the activated effector cells.
Reference:
1. D. Tang., et al., “PAMPs and DAMPs: Signal 0s that Spur Autophagy and Immunity,” Immunol Rev 249(1): 158-175, 2012.