How do I tranfer contacts from iphone to gmail?
There are a few ways to do this, but the primary way is actually built into iOS. I gathered the information from this link where there is a good comprehensive run down of all the details in setting up a Gmail account and getting the syncing going.

Set up Gmail using the Exchange trick so that contact syncing will continue in the future:


  
  Open the Settings application on your device's home screen.
  Open Mail, Contacts, Calendars.
  Press Add Account....
  Select Microsoft Exchange
  In the Email field, enter your full Google Account email address.
  Enter your Google Account password as the Password.
  Press Next at the top of your screen.
  Enter m.google.com in the Server field.
  Leave the Domain field blank.
  Enter the user email address as the Username.
  Press Next at the top of your screen again.
  Select the Google services (Mail, Calendar, and Contacts) you want to sync.
  


And then to transfer your contacts to Google it looks like you will need to transfer your current contacts to iTunes:


  If you have all your contacts on your device and want to import them into Google, you can synchronize your Google Contacts with iTunes. All Contacts and Calendars that you sync with iTunes will be put into the On My iPhone Contacts and Calendar groups on your phone. iOS currently doesn't allow you to move Contacts or Calendar events from the On My iPhone groups to the Google Sync groups.


Note that you will have to turn off iCloud contacts sync if you sync with iTunes. It won't let you transfer that to iTunes otherwise.



For another option you can use this app to transfer your contacts to Gmail, and manage syncing.



This looked like a good tutorial on setting up the sync using Exchange.