How does HIV know to attack specific immune system cells?
The CD4 receptor is a protein complex that harbors very specific chemistry. A virus is able to bind with the receptor if it harbors a particular protein/set of proteins that are able to interact with the receptor. The virus is basically disguised as something the CD4 receptor recognizes, and from there it is internalized within the cell via a process called endocytosis. You can google viral endocytosis to learn more.