So I don't think the other answers, although good, actually address why a female M (they're more about why Judi Dench to which the answer is Stella Rimmington). To answer why a female M, we need to look at both the history of Bond pre-Judi Dench, and also the film GoldenEye itself.
Prior to GoldenEye the previous two Bonds had been a mixed success. Roger Moore had been popular in the role but his advancing age (58 in a View to A Kill) and an overkill of gadgets (Moonraker is basically Bond in Star Wars), had led the makers to move to a more Fleming-esque line of films: more plot, less gadgets, more stunts. This had mixed results (For Your Eyes Only and Octopussy are seen by many as minor Bond films), and Moore's swansong (A View to A Kill) returned to earlier themes (look at Zorin's fold out map dastardly plot reveal on the airship and his plan to become dominant in his field by nuking the competition ala Goldfinger).
Post AVTAK, we had Timothy Dalton, and again a more Flemingesque, plot driven, pair of films, again with mixed success (did ok at the box office, but again regarded by many, but not all, as minor Bond films). There was then a big hiatus due to legal action (the Danjaq stuff about Thunderball rights) which meant it was nearly 10 years until a new Bond and film appeared (GoldenEye).
At this point to look at GoldenEye we see a number of points.
The film was a change of direction (again), but this time there was an attempt to bridge the two worlds of Fleming fans and Gadget Fans. This was done by bringing in a plot which was again similar to Goldfinger, but also made some decisions to span the worlds:
- A suave Bond in Pierce Brosnan, evoking Sean Connery;
- A conflicted Bond ala Timothy Dalton;
- Still a number of quips and side seductions ala Roger Moore;
- Retro elements such as the Aston Martin;
There are also modernising elements, to account for the later decade in which the film is made:
- An updated storyline, taking into account the Soviet decline;
- The impregnable bank is electronic, not a physical place;
- The lead villain is an ex-ally turned traitor;
- The indestructible henchman is a sexy woman (Xenia Onatop!);
- The female lead (Natalia) is much stronger and survives much worse (the Severna attack, and return to Moscow) on her own without Bond's help;
- The film abandons John Barry for music, using French artist Eric Serra (although you can hear this is abandoned halfway through to return to stock Bond music, and this finally worked when David Arnold came in in Tomorrow Never Dies)
- Bond himself is seen as an anachronism, a blunt instrument and misogynist dinosaur;
- A stronger and less sappy Moneypenny (although she ends up as much in love with Bond).
In this realm of change it is easy to see that with so many strong female characters, it's obvious that a female M would be a part of it. That they had Judi Dench/Stella Remington was a bonus.
As the Brosnan films went on, they ended up drawing back to the Moore days of gadgets, and when another hiatus due to studio financial issues caused them to start yet again, this time it was a full revamp of a young Bond, partly due to the only novel being left being the first, Casino Royale.
In this context the Judi Dench M was subtly changed to be a comforting carry over, AND a surrogate mother to the younger, less experienced Bond.