Uphill
Obviously the safest way if it's too steep to ride then get off and walk. Riding out of the saddle means that you will be occupying more road space (also means I can't see following traffic in my mirror). Get into the right gear while you can still turn the pedals (Shimano or Campagnolo). Keep your transmission in good condition so that the chain doesn't slip or jump. This means new chains, cassettes, chainrings as necessary.
If the road is quiet and you can tell it's safe to do so you can zig-zag up the hill to reduce the gradient but watch out for toe overlap, i.e. your foot hitting the front wheel and bringing you off the bike.
A cattle grid in the middle of the hill or mud will undo your best efforts as you will simply spin the back wheel.
Downhill
We all go too fast at times such that we can't stop in the distance we can see to be clear. For instance on Thursday I came down from the summit of Bishopdale and reached 44 mph on the top section before braking for the bends. If there had been a tractor across the road hidden out of sight round a bend further down I doubt that I would have been able to stop completely. THere is a classic tale in this month's Cycling Plus where the author came down Nant Gwynant trying to beat his speed record and ended up in the back of a sheep transporter, having shot up the ramp. On the other hand the other week I came down the hill from the top of Coverdale into Kettlewell which is a long one steepening to 1 in 4 with hairpin bends under control at 10 mph because of the ice that was present in places on the unsalted road, and I met 2 cars coming the other way on one of the bends.
I like to think that I have assessed the risks and acted accordingly. I used to be somewhat reckless but then I fell off.
I am more circumspect now.