What determines the shape of the zero curve? Why is it sometimes downward sloping sometimes upward sloping and sometimes blogpartly upward sloping and sometimes partly downward sloping?

Lot of theories have been proposed but the simplest one is the expectation theory which conjectures that long-term interest rates should reflect the expected future short-term interest rates. More precisely, it argues that the forward interest rates corresponding to a certain future period is equal to the expected future zero interest rate for that period. 

Another idea market segmentation theory conjectures that there need be no relationship between short-term, medium term and long-term interest rates. Under the theory the major investor invests in bonds of certain maturity and does not readily switch from one maturity to another. The short-term interest rate is determined by the supply and demand in the short-term bond market, the medium term interest rates is determined by supply and demand in the medium term bond market and so on .

The theory somewhat appealing is liquidity preference theory that argues forward rate should always be higher than expected future zero rates. The basic assumption underlying the theory is that investor prefers to preserve their liquidity and invest funds for short periods of time. Borrowers on the other hand usually prefer to borrow are fixed rates for long periods of time, IF the interest rates  offered by the banks and other financial intermediaries corresponding to the expectation theory , long-term interest rates would equal the  average  of expected future short-term interest rates.  In the absence of any other incentives to do otherwise investors would tend to deposit their funds for short-term periods, and borrower would lend to choose to borrow for long-term periods.

The Financial intermediaries would them find themselves financial substantial amount of long-term fixed loans with short term deposits. Excessive interest rates risk would result. In such scenarios financial intermediaries raise long-term interest rates relative to expected future short-term interest rates.

This strategy reduces the demand for long-term fixed rate borrowings and encourages investors to deposit their fund for long terms.

Liquidity preference theory leads to a situation in which the Forward rates are greater than expected future zero rates. It is also consistent with the empirical result that yield curves tend to be upward sloping more often than they are downward sloping.