you can get it from having sex but it doesn't necessarily come from that either. almost everyone is a carrier of it but in few does it actually develop into a virus. obviously if you have had sex with someone that has it there is a larger chance of getting it.
Actually, once you have skin to skin contact with someone who has that strain of HPV, you are exposed to it. The virus is there. Many people are able to combat the virus so that it doesn't cause negative effects (like cancer). The virus is there, though.