is vital in trials riding
especially when riding near an edge.

Well, practice to keep the bike still, by shifting your weight around.
I know, it sounds all too easy, but after a while, you can stand still...with no time limits...
Kenny on a rail.
A good exercise is to follow some lines
Good brakes a must

How to practice balance?
Ride very slowly upwards a very smooth slope, and go slower until the bike actually stops without the brakes. Then try to maintain the bike on the same spot by adjusting the pressure on the driving pedal (right foot most of the time). When you feel that you are loosing balance, kick in the pedal to carry on or twist the handlebars to get extra balance. The next step to understand balance better is to let the bike go backwards in the same slope, by decreasing the pressure in the pedals. This exercice is easier on a medium gear ratio.

The next step is to hop around on the back wheel, with the rear brakes on....or on the front wheel (endo). You'll need it all the time for accurate moves, to turn the bike around.

Just make sure that you have excellent brakes, so that the wheels don't cheat on you.

Fat low pressure tyres help.