Wikipedia defines "rape shield laws" as "a law that limits a defendant's ability to cross-examine rape complainants about their past sexual behavior". More accurately, these laws govern the admissibility of evidence of an accuser's sexual history in a criminal trial for rape.
There are a lot of myths about what rape shield laws do and don't do. To understand them better, we need to look at the rules of evidence. Because each state has its own rules of criminal procedure and evidence, I'll refer to the Federal Rules of Evidence ("FRE"), which are used as a template by many states.
When a court looks at admitting something as evidence, it takes a number of factors into account. The most important is relevance, defined as "evidence having any tendency to make the existence of any fact that is of consequence to the determination of the action more probable or less probable than it would be without the evidence". (FRE 401) In other words, does this evidence help us determine whether or not a fact is more or less likely to be true?
The rules also consider the reliability of the evidence. Law students tear their hair out over the complicated and often arcane rules regarding hearsay evidence, because those rules are intended to keep out evidence that may be very relevant, but which is very unreliable. If I testify that my mother's best friend's dog groomer's ex-boyfriend said that he saw the defendant shoot Jimmy Hoffa, that would be very relevant if the defendant were on trial for shooting Jimmy Hoffa–it's an eyewitness identification of the killer. But it's not reliable.
Reliable and relevant evidence may still be kept out for reasons of good public policy. One example is the rule that subsequent remedial measures are not admissible to show liability. (FRE 407). The reason for this rule is not that such evidence is unreliable or irrelevant; it's that we don't want people to refrain from fixing things because they're afraid doing so will be used against them in court. An example would be if I slipped on your icy stairs and hurt myself. I can't show you were at fault by introducing evidence that you put salt on the stairs after I got hurt. If I could, it would discourage you from salting your stairs–and then maybe somebody else would slip and get hurt.
Another policy is the idea of "more prejudicial than probative"–which is legalese meaning that evidence may be relevant and reliable, but it will so prejudice the jury or judge that the negative effect of that prejudice outweighs any value it might have. This is the objection generally raised in murder trials, when the prosecution wants to show photographs of the crime scene or the victim's body; the defense fears that a jury will look at the horrible images and be so inflamed that they will convict the defendant no matter what.
Judges weigh all these factors, and which rules do or don't apply, when deciding whether to admit evidence. The function of "rape shield laws" is to make explicit where the decision should come down on evidence regarding the accuser's sexual history. FRE 412 is the federal version of these laws. The policy is pretty obvious: You can't try to win a case by calling the accuser a slut, or intimidating the accuser by prying into his or her sexual history.
FRE 412(a) applies to all cases, civil or criminal. It says that evidence of the accuser's sexual behavior or predisposition are not admissible. FRE 412(b) makes exceptions for criminal cases, where some evidence of sexual behavior is really relevant: for example, that a sexual partner other than the defendant was the source of semen or an injury. Evidence of the accuser's reputation may be brought in if the accuser offered evidence of that reputation first–what lawyers call "opening the door" to evidence. There's also an exception in civilcases if the evidence is more probative than prejudicial, and that probative value "substantially outweighs the danger of harm to any victim and of unfair prejudice to any party".
Finally, 412(c) prohibits surprise and requires hearings on the admissibility of evidence to be conducted in the judge's chambers ("in camera"). This means that a defense attorney can't suddenly bring up the accuser's alleged sexual history and argue its admissibility in front of the jury, expecting that the jury will hear it whether or not it's admitted. (In such cases, judges admonish the jury to disregard the improper evidence. As one experienced attorney of my acquaintance says, "Juries think that's a hoot.")