Definition
In Korean, palatalization (구개음화, gugaeeumhwa) happens when a word ending in the sound ㄷ (d) or ㅌ (t) meets a grammatical ending that begins with the vowel ㅣ (i). The ㄷ or ㅌ then turns into ㅈ (j) or ㅊ (ch), sounds made at the roof of the mouth.
Why is 굳이 said "guji" and not "gudi"?
When we walk somewhere, we don't take the long way around. We naturally pick the shortcut. Your mouth and tongue do the same thing when you talk. They quietly look for the easiest path to save energy.
Try reading 굳이 (guji, "necessarily" or "going out of one's way") exactly as written: [구디] (gudi). Your tongue tip taps the ridge behind your upper teeth, then has to swing up high toward the roof of your mouth. It's a busy, awkward trip. So the tongue skips the long haul and slides to a spot close to where the next vowel, ㅣ (i), is made.
As a result, the ridge sound ㄷ (d) glides into ㅈ (j), a sound made near the roof of the mouth, and the word comes out as [구지] (guji). Palatalization is the most efficient pronunciation shortcut a lazy tongue could find.
The secret is a move inside your mouth
Run your tongue lightly along the roof of your mouth. Just behind your front teeth you'll feel the hard ridge. Farther back, around the middle, you'll feel a wide, hard ceiling. That is the hard palate. In Korean it's 구개 (gugae, from Chinese characters) or 센입천장 (senipcheonjang, "strong roof of the mouth").
The consonants ㄷ (d) and ㅌ (t) are made when the tongue tip hits the ridge behind the upper teeth. The vowel ㅣ (i), on the other hand, is made when the middle of the tongue rises high toward the hard palate.
So when ㅣ comes right after ㄷ or ㅌ, the tongue has to touch the ridge and then, in an instant, lift its whole body toward the hard palate. That's a big movement. To lighten the load, the speech organs switch to ㅈ (j) or ㅊ (ch), which are already made at the hard palate. This is why 같이 (gachi, "together") is said [가치] (gachi) and 해돋이 (haedoji, "sunrise") is said [해도지] (haedoji).
To be more precise: it doesn't happen every time
Palatalization doesn't fire every time ㅣ follows ㄷ or ㅌ. It only kicks in when the ㅣ sound belongs to a grammatical piece, such as a particle or a suffix that marks a relationship or adds meaning.
For example, in 밭이 넓다 (bachi neolda, "the field is wide"), the -이 after 밭 (bat, "field") is a grammatical particle. So palatalization happens, and it's said [바치] (bachi). But in 밭이랑 (bannirang, "furrow in a field"), 이랑 (irang) is a real word with its own meaning. A different rule applies instead, and it's said [반니랑] (bannirang).
Also, inside a single word root, modern Korean doesn't palatalize. 마디 (madi, "a joint or knot") is not said [마지], and 견디다 (gyeondida, "to endure") is not said [견지다]. The usual explanation is that when palatalization was active, the sound after ㄷ in these words wasn't ㅣ (마ᄃᆡ, 견듸다). It changed later into the shape we have now.
Common misconceptions
Words like 마디 (madi, "joint") and 잔디 (jandi, "lawn") will palatalize eventually, so they should be said [마지] (maji) and [잔지] (janji).
In modern Korean, palatalization doesn't happen inside a single morpheme, as in 마디 and 잔디. It only happens when a grammatical -이, such as a particle or suffix, attaches to a word ending in ㄷ or ㅌ.
Where you meet it
When you write about the sun rising, you spell it 해돋이 (haedoji, "sunrise"), but you say [해도지].
You write 같이 (gachi, "together") in "let's go together," and when you say it out loud, it's [가치].
You spell mailing a letter as 붙이다 (buchida, "to attach, to post"), but it sounds like [부치다].
In one sentence
Palatalization is when ㄷ or ㅌ meets the ㅣ of a particle or suffix and turns into the roof-of-the-mouth sounds ㅈ or ㅊ, to make speech easier.