I am unable to understand how does galvanic isolation work either via an optocoupler or a transformer. In an optocoupler, the input side current caused LED to emit infra red light which basically switches on the photo transistor on the output side. In case of any voltage surge on the input side or any noise, the input current will vary henceforth the light output and thus the collector current on the output. So how does isolation works? Or am i missing something really basic. Similarly for the transformer.
The point of isolation is to provide a facsimile on the output of what appears on the input yet floating from the input.
For example you want to generate a 5V sine wave from the mains. A suitable transformer will do this for you and will allow you to tie one side to earth making an inherently safe voltage. Without the isolation the output could be potentially fatal.
In other cases we do not need an accurate representation of the input just a digital signal to say for example "Is the mains more than 90V?". An Opto is likely to be used for this but would allow this information to be passed safely to the output.
Current cannot flow from one side to the other. Sure, current on one side can control current on the other, but the LED in an optoisolator can sit 100 volts above or below the phototransistor without causing any problems. Really, the whole point is to be able to pass some sort of information (analog or digital) across an isolation barrier that current doesn't cross.
Take an isolated switching power supply, for example. They will usually contain a transformer and an optoisolator. The primary side of the transformer will be connected to mains voltage through a switching transistor and the secondary side will be connected to a load of some sort, perhaps a computer or cell phone. Obviously you don't want your computer case electrically connected to the 120 volt supply, as this can kill you if you provide a path to ground. So the transformer galvanically isolates the primary and secondary, allowing energy to pass through in the form of an oscillating magnetic field while preventing any current from flowing across the isolation boundary. To efficiently regulate the output voltage, the switch (on the line side) needs to somehow be connected to the load side so it can control the output voltage. An optoisolator serves to provide this feedback connection by converting the output voltage to light, which passes back through the isolation barrier without allowing current to flow across the barrier. The end result: a safe, efficient, isolated power supply that won't kill you if you provide a path from the output to ground.