I was going to post a link to MFOS, but the site appeared down and then I got distracted by a shiny object ;-)
Anyway, the key to it is this - you have two resistors, and two diodes. The diodes and resistors are wired in series, with one diode the opposite way round from the other, and then the diode/resistor pairs are wired in parallel. One end of this then goes to the gate signal, and the other goes to a capacitor and is buffered by an opamp. The other end of the cap is connected to ground. Have a look for this arrangement in any ADSR circuit, including the AD envelopes on the MFOS page.
How it works is this: When the gate is low, the capacitor discharges through the diode with the cathode nearest the gate pin, through the resistor. If that's a variable resistor, you can vary the discharge time. No resistance = instant discharge, big resistance = slow discharge. Simple, eh?
When you then pull the gate input to the supply rail, the cap charges through the other diode and resistor. Again, if there's only a little resistance it will charge instantly, but if there's a big resistance it will charge slowly.
When the envelope level reaches maximum, one comparator goes "Aha! We've hit the top of the envelope" and its output switches on, pulling the gate pin to ground and starting the decay phase. Some synths just have an AD envelope, which usually has a switch to control this - you can have it do the attack phase then the decay phase, or you can have it do the attack phase and wait until the gate goes low again to do the decay phase (which acts like a release phase).
So how do sustain knobs work? Well, the sustain knob forms a voltage divider. This feeds a comparator, which is connected to the envelope output. The output voltage begins to fall in the decay phase, but at some point the comparator connected to the sustain level control will switch - "Oh now the level has fallen below my setting" - and disconnect the gate input entirely. With the A and D resistors no longer connected to anything, the capacitor has no way to discharge (well, it will gradually leak away but we can mostly ignore that). As long as we hold the gate on, the comparator and its associated flipflop will keep the A and D resistors disconnected, and the output from the envelope is whatever the capacitor charged to.
When we let go of the key, the gate input goes low, and the circuit switches the release resistor in across the cap, allowing it to discharge. This works pretty much like the A and D resistors.
Although an ADSR envelope circuit looks complicated when you see it drawn out ("What are those AND gates doing? Why so many analogue switches?") it's really pretty straightforward. If you start by looking at an AD envelope circuit, it should become clear.