It can be difficult to determine what caused an infection, but a workup that includes a physical exam, diagnostic testing such as blood work, and a medical history usually yields a diagnosis.
How the infection is treated depends on the type of infection and its severity. Viral infections, such as influenza and HIV, can be treated or better managed with antiviral medications.
Antibiotics are often prescribed to fight bacterial infections, typically over the course of a few days to a couple of weeks.  Unfortunately, some bacteria have developed a resistance to antibiotics and other treatments are necessary.