The code can be found in GetMinFee() in
main.cpp.
First a fee is calculated by dividing the size of the transaction (in bytes) by 1000, adding one to the integer quotient, and multiplying the sum by 0.0005 (nBaseFee) . . .
int64 nMinFee = (1 + (int64)nBytes / 1000) * nBaseFee;
If the total size (in bytes) of the transaction is less than 10kB, then the client lets you send it without the fee . . .
if (nBytes < 10000)
nMinFee = 0;
However, if any output is less than 0.01, then a the client decides you need a fee after all . . .
if (txout.nValue < CENT)
nMinFee = nBaseFee;
After all that, if the fee is bigger than 21,000,000 BTC, then it gets reduced to exactly 21,000,000 BTC.
if (!MoneyRange(nMinFee))
nMinFee = MAX_MONEY;