HIV damages your immune system because it targets CD4 cells. The virus grabs on to the surface of a cell, gets inside, and becomes a part of it. As an infected CD4 cell multiplies so it can do its job, it also makes more copies of HIV.
Those new bits of virus find and take over more CD4 cells, and the cycle continues. This leads to fewer and fewer HIV-free, working CD4 cells.
HIV can destroy entire “families” of CD4 cells, and then the germs these cells fight have easy access to your body. The resulting illnesses are called opportunistic infections because they take advantage of your body’s lack of defense.