Every time someone uses search terms on Google, an auction takes place in Google Ads (previously known as Google AdWords) between all who bid for related keywords in order for their ads to appear in the search results. This auction determines the ad position and each advertiser's Cost-Per-Click (CPC). Two things are important here:
1) The amount you are willing to bid based on your max. CPC.
2) The Quality Score of your ad based on the specific search query.
Here is a video, where Hal Varian, Google's chief economist, explains the bidding process in further detail.