Київ is the nominative and accusative case, у Києві is the locative case. :) Compare Львів 'Lviv/Lvov' — у Львові 'in Lviv', Харків 'Kharkiv/Kharkov' — у Харкові.
Nominative case is used when the noun is the grammatical subject of the sentence:
- Київ гарний. 'Kiev is nice.'
Accusative case is used when the noun is the object of the sentence:
- Я люблю́ Ки́їв. 'I like Kiev.'
And locative case is used after certain prepositions:
- Я в Ки́єві. 'I'm in Kiev.'
Ukrainian «о» and «е» sometimes transform into «і» in closed syllables. And when «є» stands for [йє], then it's transformed into [йі] and this is written with the letter ї (this is what happens in Київ).
However, the transformation only occurs in closed syllables: Ки-їв, їв is closed (it ends in a consonant в). But in prepositional case it becomes an open syllable: Ки-є-ва (є is an open syllable, it ends in a vowel; в belongs to the next syllable here) so this transformation doesn't occur and we see the original vowel.
This is related to the language history. In the past, all syllables were open, and Київ had an extra-short vowel ъ in the end: Києвъ. Later that ъ disappeared, but it triggered a vowel change in the previous syllable. But Києві never had ъ, so nothing disappeared and no phonetic changes happened.
How do I decide when to use в versus у? I know it depends on which sound "flows" easier in the phrase, but do I decide based on the consonant/vowel preceding the в/у or the consonant/vowel following the в/у?
For example, in this exercise,
працювала у Києві,
in the "а у к...,"
Does the "а у" flow better than if it would've been "в к"
працювала в Києві?
The problem is, those rules don't really reflect the actual usage. In another discussion, I've linked a table comparing the rules with the usage in the Ukrainian classics. The rules are biased towards more «у» than Ukrainian actually uses. E.g. they require starting the sentence with «уранці» when «вранці» is perfectly fine.
Ukrainian Duolingo course is evern more strongly biased towards «у»: Duolingo introduces «у» in sentences like this one, where the rules require «в». I don't know why. Maybe the Duolingo authors didn't know those rules. Maybe they thought the у/в alteration is too complex for foreigners and is not so important, so they can do without it. Maybe for some other reason.
Anyway, when unsure, you can use the rules as a good starting point.