Julian Sanchez succinctly explains the rational basis test as applied by the courts:
Now, understand: For a law to be “rationally related” to a legitimate state purpose, it’s not necessary that it actually achieve that purpose, let alone achieve it without bringing about various ancillary harms in the process. It’s enough that a sane legislator might reasonably believe it to contribute to the relevant goal.
In other words, whenever the Supreme Court strikes a law down while claiming to apply the “rational basis test,” they weren’t actually applying the rational basis test. Case in point: Romer v. Evans, in which the court laid the foundations for Lawrence v. Texas by essentially applying heightened scrutiny to discrimination against gays and lesbians—even though they claimed they were simply applying the rational basis test.