How does the immune system recognize harmful entities?

The innate immune system marks objects as “harmful” by recognizing specific molecular patterns. These DAMPs (damage-associated molecular patterns) and PAMPs (pathogen-associated molecular patterns) initiate cell signaling cascades which trigger the innate immune response.1

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.