It sounds like you are asking e.g. "why do we have a suffix -s for the plural", "why do we have a prefix un- for negatives", etc. The alternative would be to mark the function with a separate word like "not, no". The alternation would then be two or more word forms. There is a sub-theme connected to this, seen rampantly in German and a bit in English, namely "umlaut" where a grammatical difference is signalled not with an identifiable affix, but with a change in sounds of the word (e.g. goose ~ geese).
There is historical process where independent words become affixes. For example, Norwegian has definite suffixes that historically developed from demonstratives. We have examples in English such as the negative inflections won't, don't, can't which arose from and still alternates with two-word sequences will not etc. This is known as grammaticalization *that is, these are examples of grammaticalization but not all grammaticalizations are phonological reanalyses of separate words). One might assume that all affixes in all languages historically derive from separate words, but that's asserting more than we have evidence for (even if it is likely to be true at some level of historical depth).
Internal-change types of morphology are essentially regular morphology interacting with regular phonology, and then something changes to mess up the phonological context for the rule. The goose~geese alternation used to be regular phonology. "Geese" derives from earlier gansiz by regular phonological rules, but then the triggering suffixes were lost through other sound changes. There was a bit of an attempt especially by Ted Lightner to preserve the historical origins of alternations in modern grammars, but it is typically recognized that children do not have sufficient basis for reconstructing the earlier form of words, when (if) the alternation was regular phonology.
There has been a protracted and frankly pointless terminological skirmish in the literature over the question of phenomena that might be called "morphological". What happens in "goose~geese" is that there is an alternation of vowels, so there is a phonological event. The conditioning factor is not a phonetic factor, it is a grammatical property, so the conditioning is morphological (if it is not actually syntactic).
So, affixes often develop from word combinations via application of phonological rules. Affixation makes possible certain phonological processes (in Germanic, vowel fronting only applies to sequences within the word). The phonological properties of affixes can be changed while the triggering changes are retained, leaving the language with a morphological (grammatical) distinction that is realized via some phonological process without recoverable phonological conditioning.