A recent result of Zheng and Tse states that over a quasi-static channel, there exists a fundamental tradeoff, referred to as the diversity-multiplexing gain (D-MG) tradeoff, between the spatial multiplexing gain and the diversity gain that can be simultaneously achieved by a space-time (ST) code. This tradeoff is precisely known in the case of independent and identically distributed (i.i.d.) Rayleigh fading, for T ≥ nt + nr - 1 where T is the number of time slots over which coding takes place and nt, nr are the number of transmit and receive antennas, respectively. For T < nt + nr - 1, only upper and lower bounds on the D-MG tradeoff are available. In this paper, we present a complete solution to the problem of explicitly constructing D-MG optimal ST codes, i.e., codes that achieve the D-MG tradeoff for any number of receive antennas. We do this by showing that for the square minimum-delay case when T = nt = n, cyclic-division-algebra (CDA)-based ST codes having the nonvanishing determinant property are D-MG optimal. While constructions of such codes were previously known for restricted values of n, we provide here a construction for such codes that is valid for all n. For the rectangular, T > nt case, we present two general techniques for building D-MG-optimal rectangular ST codes from their square counterparts. A byproduct of our results establishes that the D-MG tradeoff for all ≥ nt is the same as that previously known to hold for T ≥ nt + nr - 1.