I know I've heard that racial discrimination when drawing district lines is unconstitutional. But where in the constitution does it say that, and are there other rules that gerrymandering has to follow to be constitutional or legal?
The 14th amendment includes:
Section 1. All persons born or naturalized in the United States, and subject to the jurisdiction thereof, are citizens of the United States and of the State wherein they reside. No State shall make or enforce any law which shall abridge the privileges or immunities of citizens of the United States; nor shall any State deprive any person of life, liberty, or property, without due process of law; nor deny to any person within its jurisdiction the equal protection of the laws.
The 15th amendment says:
Section 1. The right of citizens of the United States to vote shall not be denied or abridged by the United States or by any State on account of race, color, or previous condition of servitude.
Section 2. The Congress shall have power to enforce this article by appropriate legislation.
The combination of these two provisions gave rise to the one person, one vote criterion. This was initially used to say that voting districts had to be approximately the same size by population. More recently, this was used to declare a North Carolina redistricting map to be unconstitutional in Cooper v. Harris. The idea was that the map drew blacks into districts in such a way as to dilute their influence, making their vote worth less.
It's unclear how this will play out long term. Traditionally, states have been required to include majority-minority districts in their redistricting plans if they could. However, Harris held that those two majority-minority districts were not permissible. Is that the new standard? Or something specific to that particular situation?
There is currently as case Gill v. Whitford that is more directly about gerrymandering. It bases its claim on the first amendment's right to freedom of association. That case could potentially set standards for measuring whether a gerrymander is permissible. Or it could lead to proportional representation, as a natural extension of its argument (proportional representation allows the maximum freedom in choosing with what voters to associate).
The controlling law is the Voting Rights Act of 1965.
Gerrymandering in general is not unconstitutional (and I have not hear anyone seriously argue that it should be).
Your question is likely spurred by recent SCOTUS looking into what is usually labeled extreme partisan gerrymandering in Gill vs Whitford, also known as Wisconsin partisan gerrymandering case.
The specific theory of the cases like that typically used in these lawsuits is that gerrymandering (racial or extreme political) violates Equal Protection Clause, part of Fourteenth Amendment:
No State shall ... nor deny to any person within its jurisdiction the equal protection of the laws
Extreme partisan gerrymandering has not yet been found unconstitutional by Supreme Court (though there's a chance they might, as per Five Thirty Eight and ScotusBlog) in any specific case, but the SCOTUS's past opinion is that it may be in theory in some cases.
unconstitutional discrimination occurs only when the electoral system is arranged in a manner that will consistently degrade a voter's or a group of voters' influence on the political process as a whole.
Davis v. Bandemer 478 U.S. 109 (1986)
P.S. Please note that racial gerrymandering has been found unconstitutional in some cases (but not others).