When there are stars around the pokemon, then you are guarenteed to get it. The special trait gutsy and certain moves make this more likely to happen. Sparkling pokemon are guarenteed to get befriended. The stars wear off, but the shiny is permanent.
EDIT: the pokemon with starts are dizzy and the shiny pokemon are sparkly.