Reasons why you can compete for a different country other than the one you grew up in:
1. Born in another country and moved at a small/any age
2. (Most of the time) Your parents were from that country
But that's pretty much it, It's up to the athlete what country they want to represent. During the opening ceremonies there was a kid who grew up in America, but they were talking about his grandparents being very irish (and assuming his parents are irish born) he decided to compete for Ireland.
I think all in all the Olympic games go to what country/ethnicity you want to represent (and have citizenship to)
Fun Fact: My brother was born in Panama, but my parents moved back to the States when he was 2. He always thought that it would be cool to compete in the winter olympics for them.