You can find the explanation in page for Pinyin on wikipedia:
Note on y and w
y and w are equivalent to the semivowel medials i, u, and ü (see below). They are spelled differently when there is no initial consonant in order to mark a new syllable: fanguan is fan-guan, while fangwan is fang-wan (and equivalent to fang-uan). With this convention, an apostrophe only needs to be used to mark an initial a, e, or o: Xi'an (two syllables: [ɕi.an]) vs. xian (one syllable: [ɕi̯ɛn]). In addition, y and w are added to fully vocalic i, u, and ü when these occur without an initial consonant, so that they are written yi, wu, and yu. Some Mandarin speakers do pronounce a [j] or [w] sound at the beginning of such words—that is, yi [i] or [ji], wu [u] or [wu], yu [y] or [ɥy],—so this is an intuitive convention. See below for a few finals which are abbreviated after a consonant plus w/u or y/i medial: wen → C+un, wei → C+ui, weng → C+ong, and you → C+iu.
Well, this note is so detailed, I really feel it difficult to add something… maybe it can be concluded as this:
When [i] and [u] sounds appear at the beginning of a Chinese character, they're written as y and w, in other cases they're written as i and u.