Origins
The prefix 'in-' in modern English is a striking case of homonymous merger: two etymologically unrelated morphemes that have come to share an identical form.βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ The first 'in-' is a negator meaning 'not' or 'without,' as in 'invisible,' 'incorrect,' or 'impossible.' The second 'in-' is a locative meaning 'in,' 'into,' or 'on,' as in 'income,' 'inhale,' 'insert,' or 'inscribe.' These two prefixes descend from distinct Proto-Indo-European roots and travelled through different languages before colliding in the vocabulary of Middle English. Native speakers almost never notice the split; etymologically, it is one of the most interesting collisions in the history of the English word-stock.
The negating 'in-' descends from Latin 'in-' (not), itself from Proto-Indo-European *nΜ₯-, the zero-grade variant of *ne (not). This same PIE root gave rise to Greek 'an-' and 'a-' (as in 'anonymous,' 'atheist,' 'anarchy'), Sanskrit 'a-' and 'an-' (as in 'ahiαΉsΔ,' non-violence), Lithuanian 'ne-,' Old Church Slavonic 'ni-,' and β crucially for English β the Germanic prefix that became Old English 'un-.' In other words, Latin 'in-' (negating) and English 'un-' are cognates: the same root, inherited twice, once through the Italic branch and once through the Germanic branch.
The locative 'in-' has a different history. It is native Germanic, descending from Old English 'in-' (from Proto-Germanic *in, from Proto-Indo-European *hβΓ©n, meaning 'in, within'). The same root produced Latin 'in' (the preposition), Greek 'en' (αΌΞ½), Sanskrit 'antΓ‘r,' and numerous locative particles across the family. In Old English this prefix freely formed compounds such as 'ingang' (entrance), 'inweard' (inward), and 'innan' (within). Through Middle English this locative 'in-' continued to form words like 'income,' 'inlay,' 'insight,' 'intake,' and 'input' β usually on native English bases.
Middle English
In Middle English (twelfth to fifteenth centuries) the negating 'in-' entered English in a flood of Latin and Anglo-Norman vocabulary. Scholastic writers and translators imported thousands of Latin-derived words, and with them the productive negating prefix: 'incorrect,' 'invisible,' 'injustice,' 'ingratitude,' 'incapable.' By the Renaissance (sixteenth to seventeenth centuries) the Latinate influx had made 'in-' one of the most common negating prefixes in English, rivalling and in many domains displacing native 'un-.'
A characteristic feature of the negating 'in-' is its allomorphy: the prefix assimilates to the first consonant of the stem it attaches to. Before labial consonants (p, b, m) it becomes 'im-' ('impossible,' 'imbalance,' 'immortal'). Before 'l' it becomes 'il-' ('illegal,' 'illegible'). Before 'r' it becomes 'ir-' ('irregular,' 'irrational'). Before most other consonants it remains 'in-' ('incorrect,' 'insincere'). This assimilation is inherited from Latin, which applied the same phonological rules.
The locative 'in-' does not assimilate in this way. 'Income,' 'input,' 'inlay,' and 'inside' keep 'in-' regardless of the following consonant, because the rule is Latin and the locative 'in-' is Germanic. This gives a rough diagnostic: a word with 'im-,' 'il-,' or 'ir-' almost always uses the negating Latinate prefix, while words starting with a plain 'in-' may use either, depending on whether the base is Latinate or Germanic.
Old English Period
A common question is when to use 'un-' versus 'in-.' In general, 'un-' attaches freely to native English adjectives ('unhappy,' 'unclean,' 'unwise') while 'in-' attaches to Latinate adjectives ('unjust' is the Old English pattern but 'injustice' follows the Latin pattern because 'justice' is Latinate). The two prefixes sometimes produce doublets with different shades of meaning: 'inhuman' (monstrously cruel) versus 'unhuman' (rare, simply not human); 'inartistic' (lacking artistic merit) versus 'unartistic' (not artistic in nature).
Representative negating 'in-' words include: invisible, incorrect, injustice, insufficient, insincere, indirect, inactive, incapable, incomplete, indefinite, indiscreet, indisputable, ineffective, inequality, inevitable, infamous, infertile, inflexible, informal, infrequent, ingratitude, inhuman, innocent, innumerable, insecure, inseparable, intolerant, invaluable, invariable, involuntary. Note that some words that look like they contain negating 'in-' actually contain the locative: 'incorporate' is 'in + corpus' (to bring into a body), not 'not + corporate.'
Representative locative 'in-' words include: income, inhale, insert, inscribe, inject, induct, induce, include, invade, invoke, inhabit, invest, incorporate, input, intake, inlay, insight, inside, indoor, inward. Many of these are Latinate borrowings where the locative 'in-' corresponds to Latin 'in' (into), so they follow Latin patterns; others are native English compounds with 'in' as preposition.
Latin Roots
A small subset of English words contain 'in-' in a third, archaic sense: emphatic or intensive, as in 'inflame' (to set on fire, from Latin 'in-' + 'flammare') or 'invaluable' (which paradoxically means 'so valuable it cannot be valued'). 'Invaluable' is occasionally used as an example of the living confusion between the two main 'in-' senses: speakers parse it as 'un-valuable' and then have to relearn the positive meaning.
Historically, 'in-' is one of the most productive prefixes in English, rivalled only by 'un-' and 're-.' Its productivity peaked in the sixteenth and seventeenth centuries during the Latin-rich Renaissance, but new 'in-' coinages continue to appear in scientific and technical registers: 'incognito,' 'in silico,' 'in vitro,' 'in vivo,' 'in absentia' (these last four preserve 'in' as a freestanding Latin preposition). Modern English speakers treat 'in-' as a live prefix and can coin new words with it freely, though new coinages now tend to prefer native 'un-' for general use ('un-fun,' 'un-cool') while keeping 'in-' for formal or Latinate registers.