"Gosh" is a softened version of "God". We have very creative ways of softening "strong" language and swear words. Another example is "gee" or "geez" for "Jesus".
As Alex has explained neatly, you use these exclamations when you are surprised or shocked. "Oh my gosh" can be a positive or negative reaction.