The drugs used for post-exposure prophylaxis in HIV are the same types of drugs used to treat people with HIV.
There are several categories of drugs. Many prevent the virus' reverse transcriptase enzyme (which converts the virus' RNA into DNA, so that it can be incorporated into the host's own DNA). Others block the protease enzyme, which is responsible for the maturation of the virus.
When you are, say, pricked with a HIV-infected needle, very few HIV particles enter your body. This is the best opportunity that the body has to beat the infection (actually, it's the ONLY opportunity your body has). The drugs given seek to prevent viral replication, thus giving your body a longer window period to potentially eliminate the infection while the virus' numbers are low.
HIV is actually poorly transmitted (you have an average 0.3% chance of getting HIV from an infected needle), and if the prophylaxis is taken quickly enough (started within the first few hours), the risk of getting HIV is probably no greater than 0.05%.