Short answer:
When the <gn> comes word-initially or word-finally, the /g/ often gets removed.
However, in word-medial position, the /g/ is sometimes pronounced when it's followed by a vowel (because it's allowed across the syllables adn the vowel splits it up into two syllables) and is not removed.
When the <gn> is followed by a vowel, the /g/ is usually pronounced except when some suffixes like -ing, -er and -able are appended. There may be lots of exceptions, however.
Examples:
When the <gn> is not followed by a vowel, the /g/ is usually silent as in the following words:
- Sign → /saɪn/
- Resign → /rɪˈzaɪn/
- Impugn → /ɪmˈpjuːn/
- Malign → /məˈlaɪn/
These words do not have a vowel after the <gn>, so the /g/ is not pronounced.
Now,
- Signature → /ˈsɪɡ.nə.tʃə/
- Resignation → /ˌrez.ɪɡˈneɪ.ʃ(ə)n/
- Pugnacious → /pʌɡˈneɪ.ʃəs/
- Malignant → /məˈlɪɡ.nənt/
These words have the /g/ because the following vowel splits up the /gn/ and makes it two syllables; /g/ moves to the preceding syllable while the /n/ moves to the next syllable.
However, some suffixes do not let the /g/ to be pronounced (I don't know the reason).
- Signable → /saɪnəb(ə)l/
- Signing → /saɪnɪŋ/
- Aligning → /əlaɪnɪŋ/ etc don't have the /g/.
Explanation:
The reason boils down to English Phonotactics (that deals with restrictions in a language on the permissible combinations of phonemes).
English Phonotactics does not permit a plosive followed by a nasal. So we cannot have an onset (beginning of a syllable) or a coda (ending of a syllable) consisting of PLOSIVE + NASAL.
Therefore, we don't have clusters like /pn/, /tn/, /kn/, /bn/, /dn/ and /gn/ etc in English because they violate the Phonotactics constraints of English.
So when the <gn> is followed by a vowel, the vowel splits up the /gn/; the /g/ moves to the preceding syllable and the /n/ moves to the next syllable.
- Signature → /ˈsɪɡ•nə•tʃə/
I don't know the reason as to why the /gn/ doesn't split up when it's followed by certain suffixes (like -ing and -able)
Greg Brooks in his Dictionary of the British English Spelling system writes:
- In a few words with final /n/ spelt <gn> /g/ surfaces in derived
or related forms: compare impugn, malign, sign with pugnacious,
repugnant, malignant, assignation, designation, resignation, signal,
signature (all with change of vowel phoneme) – but /g/ does not surface
before inflectional suffixes, as in impugns, impugning, impugned,
maligns, maligning, maligned, signs, signing, signed.
- In three words with final /m/ spelt /g/ surfaces in derived or
related forms: compare paradigm, phlegm, syntagm with paradigmatic
(with change of vowel phoneme), phlegmatic, syntagma(tic) – but /g/
does not surface in paradigms, phlegmy.
But it doesn't explain why the /g/ is not surfaced in those inflectional words.
Another relevant quote from An Introduction To Language by Victoria Fromkin, Robert Rodman, Nina Hyams:

Another relevant quote from Nathan (2008:82):
Nathan (2008:82) asserts that not only can segments be deleted, sometimes they can be inserted instead. There seem to be two basic reasons for insertion: preventing clusters of consonants that violate syllable structure constraints in the language, and easing transitions between segments that have multiple incompatibilities — Research Gate
Why is 'signature' not pronounced [saɪgnat͡ʃə]:
Why is 'signature' not pronounced with a long vowel in the first syllable?
It's because of a fairly common phenomenon called Trisyllabic Laxing, it is a process whereby a tense vowel in a stressed syllable is shortened if two (or more) syllables follow.
Examples:
- Sign, signature --- /saɪn/ -> /ˈsɪɡ.nə.tʃə/
- Malign, malignant --- /məˈlaɪn/ -> /məˈlɪɡ.nənt/
- Profane, profanity --- /prəˈfeɪn/ -> /prəˈfæn.ə.ti/
- Sincere, sincerity --- /sɪnˈsɪə/ -> /sɪnˈser.ə.ti/
- Impede, impediment --- /ɪmˈpiːd/ -> /ɪmˈped.ɪ.mənt/
- Divine, divinity --- /dɪˈvaɪn/ -> /dɪˈvɪn.ə.ti/
Another thing I've noticed about these words is that the words in which the /g/ is pronounced in <gn> combination have a short vowel before the <gn> and the words in which the /g/ gets removed have a long vowel/diphthong before the <gn>
Examples:
Long vowel before the <gn>:
- Sign → /sʌɪn/
- Signage → /ˈsʌɪnɪdʒ/
- Signable → /ˈsʌɪnəb(ə)l/
- Assignable → /əˈsʌɪnəb(ə)l/
- Resign → /rɪˈzʌɪn/
- Design → /dɪˈzʌɪn/
Short vowel before the <gn>:
- Signet → /ˈsɪɡnɪt/
- Signature → /ˈsɪɡnətʃə/
- Signal → /ˈsɪɡn(ə)l/
- Malignant → /məˈlɪɡnənt/