"Optimal" cadence varies with what you're trying to optimze, so your question doesn't have a simple answer.
Freely-chosen Cadence vs. a Targeted Cadence
A recent review by Hansen et al. summarizes what is currently known about factors affecting choice of cadence. In particular, they conclude "[d]uring high-intensity cycling, close to the maximal aerobic power output, cyclists choose an energetically economical cadence that is also favorable for performance. In contrast, the choice of a relatively high cadence during cycling at low-to-moderate intensity is uneconomical and could compromise performance during prolonged cycling." The first sentence means that experienced cyclists freely choose the cadence that produces good performances and they don't need to have someone dictate what that cadence is. The last sentence means that having an inappropriate cadence forced on you can be detrimental to performance.
Cadence and Power
The relatively recent proliferation of on-bike power meters that record both power and cadence has helped provide additional data that bears on the issue. By definition, a cyclist's power output = cadence * crank torque * a conversion constant (the conversion constant depends on the units you use for measuring power, cadence, and torque; if you measure power in watts, cadence in radians per second, and crank torque in Newton meters, the conversion constant is 1). If you are racing at high power, or riding leisurely at low power along a bike path with your children, or on a spirited ride with friends, you will certainly choose different power levels; what has become clear from the data is that riders, even very experienced ones, choose different combinations of cadence and crank torque to match higher or lower levels of power. They do not always choose a fixed cadence and change gears in order to keep in robotic lockstep with that number.
Cadence and the Type of Ride
But even if we exclude leisurely riding and focus only on racing (at high power), cadence and crank torque vary according to the type of race. Here are cadence-crank torque plots for the same (domestic pro) rider in three different types of races: a road race, a criterium race, and a time trial. Torque is measured in Newton-meters, while cadence is measured in rpm. The thin red dotted lines show the combinations of cadence and torque that produce 300, 500, and 700 watts. As you may be able to see, the three different types of races call for different combinations of cadence and crank torque. The time trial shown here was done at a relatively steady power level but the road race and criterium race were much more variable. For those races, the rider attained higher power by increasing both cadence and crank torque. This is a quite common pattern for road racing, and it helps to explain why observers often remark that racers pedal at high cadence: the racers are also pedaling at high power and high crank torque but the only visible clue is the high cadence. This, then, is the basis for the sentences quoted from Hansen et al. above: the cadence used to produce high power appears to be neither economical nor performance-enhancing at low power. Just because you see a ProTour rider pedal at X rpm doesn't mean you should pedal at X rpm (unless you also produce ProTour levels of power).

Cadence and Terrain
Your question also asked whether cadence varies with terrain. Here is a plot of cadence and torque for a rider who was doing a set of hill intervals. The upper left panel shows his cadence and torque over the entire ride. The upper right panel shows the elevation profile for his ride; as can be seen, the ride was slightly rolling from his house out to a hill which he climbed and descended four times, then he returned home over the rolling road. That upper right panel marks the climbing portion of his ride in red. The bottom two panels show his cadence and torque for the corresponding black and red portions of the ride. As before, the thin dotted lines (this time, in blue) show "isopower contours." Clearly, he used different combinations of cadence and torque on the climbing sections than on the descending and rolling sections.

To emphasize this point, here is a plot that shows cadence vs. estimated road gradient for ProTour rider Gustav Larsson during Stage 3 of the 2009 Tour of California. As you can see, even if we exclude periods of coasting, his cadence varied from around 20 rpm up to around 120 rpm, and as the road gradient became steeper, his cadence decreased.
Cadence and Crank Torque
Do you wonder about the relationship between cadence and crank torque? Here is a plot showing another rider in a "pure" hill climb. The upper left panel shows the relationship between cadence and power; the upper right shows the relationship between crank torque and power; and the bottom two panels show the relationship between cadence and crank torque, one with and one without isopower contours. The bottom panels make clearer that there is often an inverse relationship between cadence and crank torque, but the upper panels show that in this instance crank torque was a larger determinant of power output than cadence.

Cadence and Knee Strain
Some riders claim that slow cadences (below, say, 60 rpm) can injure knees. However, slow cadence cannot by itself injure knees; as you sit at your desk reading these words, your “cadence” is almost surely close to zero but the force on your knees is also low. Riders who make these claims are conflating low cadence with high force. From the plots provided it should be clear that one of the simplest ways to expose the knees to lower force is simply to ride at lower power. Riding at 60 rpm at low power can be done with low pedal force; riding at 90 rpm at high power must be done with high pedal force. So level of power output (also known as “workload”) is a key to understanding joint strain. In the mid-1980s, M. Ericson published a series of studies examining forces on the hip, knee, ankle, foot, and the leg muscles during cycling including this one. Importantly, he concluded, “[o]f the four parameters studied (workload, pedalling rate, saddle height, pedal foot position) workload was the most important adjustment factor for change of joint load and muscular activity. An increased pedalling rate increased the muscular activity in most of the muscles investigated, generally without changing the joint load. Increased saddle height decreased the maximum flexing knee load moment, but did not significantly change the flexing hip or dorsiflexing ankle load moment. Muscular activity in most of the muscles investigated was not generally changed by different saddle heights.”
Conclusion
The conclusion from all of this is that cadence varies both with the ride and with the rider. In that sense, asking about "optimal cadence" is, in isolation of the characteristics of the ride and rider, a red herring. Without the proper context, asking about optimal cadence is like asking about optimal power, or optimal crank torque.