Exclamations like "oh my goodness", "goodness gracious" or "oh my dear" are all meant to express surprise mainly, but usually with the connotation of slight feelings of scandalization. It is a polite way to respond to being surprised or shocked, rather than yelling or cursing.