I know that media are an attachment post type that are children to a post, but how are galleries stored in WordPress?
This completely depends on how your gallery shortcode looks like.
If you replaced it with something custom, then you'll have to take a look at the plugin (or the theme).
The default gallery shortcode gets it from a query:
Then it decides to add additional args for either
Here're the 3 scenarios that add the additional/non-shared arguments.