There are specs here for the w3g file:
http://w3g.deepnode.de/files/w3g_format.txt
And the replay actions documentation:
http://w3g.deepnode.de/files/w3g_actions.txt
Essentially, you just follow the format. You can have a struct defined with the dword's and bytes (according to the file specification), and then you read N bytes into the struct, and then you interact with the data.
But I don't know if/how you can check gold. The replay format is actually one of the longer file formats. You may want to look into "Replay Parsers", since they can do the blunt of the work for you.
As for the winning circumstances, that requires a map-specific implementation. The w3g isn't as friendly as a wc3 interface, so you can't necessarily ask it things at random. It has a schedule of "actions" which you can refer to. As such, you can abuse these actions to tell your parser such circumstances. For example, let's say the replay file keeps track of orders. If you want to send a message "40" to represent the winning score, you can order something 40 times and detect it in your replay parser. Of course, it isn't quite that black and white.
Here are some useful links:
http://www.wc3c.net/showthread.php?t=108962
^Just a replay explorer, you may want to check it out. And of course, w3mmd:
http://www.wc3c.net/showthread.php?t=104567
Basically, it provides an interface for doing what I described. You can output some arbitrary "data" that can be subsequently read from the w3g file. Perhaps Replay Explorer can work in combination with it? I don't know. I'm really not an expert on the topic, so that is the best info I can give.