You need to mount google drive to your Colab session.
from google.colab import drive
drive.mount('/content/gdrive')
Then you can simply write to google drive as you would to a local file system like so:
with open('/content/gdrive/My Drive/file.txt', 'w') as f:
f.write('content')