The word shag has many meanings. It can mean rough or tussled hair, it can refer to a certain type of cloth (as in shag pile carpet), and it can also be used as a noun for an act of copulation or referring to someone who is sexually attractive. But it is most well-known as British slang for having sexual intercourse. It is considered vulgar slang but not as obscene as the more offensive word fuck. It turns up in a lot of the more raunchy late-night British TV programmes and is even a trademark of some stand-up comedians. It is less common in America though.
It is not clear how the word shag got its current meaning, but it seems that it became a slang term for sexual intercourse by replacing words like fuck and making love. This is a relatively recent development and probably happened around the 1990s or so. As with a lot of English slang, it is not very precise and can be used in a variety of different ways. It is often confused with the word sex, which has a similar meaning but in this case is a more explicit description of sexual activity.
Another interesting thing about this word is that it can be a verb as well as a noun. If a basketball coach tells his players during shooting practice to shag all the balls then this means to collect all the missed shots and return them. This is a similar expression to the one that can be heard in baseball where coaches might instruct their players to "snag" all the fly balls they hit into the outfield.