First Rule: Leave it like you found it. If it was open, leave it open. If it was closed - close it again.
When in a big group:
The Ride Leader stops at the gate and the second rider gets off, opens the gate and lets the WHOLE group through. Once the sweeper is through he closes the gate and head off in front of the sweeper. The sweeper always rides behind the group. At the next gate the new rider in the second position will do the same. This way the opening the gates are divided equally between the riders and the ride leader and sweeper can concentrate on their duties. If you migrate up the order by passing some riders / being among the first to leave after a stop that's fine, but your next turn to open a gate will also come up quicker.
Among a group of friends:
Here groups usually agree on their own thing. These are the two systems I think work best:
The "D" formation. This is similar to the system above. The first rider to reach the gate opens the gate and lets the whole group through before closing the gate and continuing. This system works well en keeps the average speed of the group up. This could be important on a tour where a lot of ground needs to be covered on a particular day. This is also a good way to make sure that all riders are still present in the group.
A different system that also works well is the stone on the gate post. This will be used in cases where the riders are a little further apart. If the first rider finds a closed gate he will open it and put a large, easily visible, stone on the gate post. He will leave the gate open and continue on his ride. When the last rider rides past he will see the stone, close the gate and remove the stone. Do not use this system if there are livestock nearby (obviously) or if the riders are very far apart. Then let everybody open and close their own gates rather.