How does mysql transaction works?
Does all of the rows affected by the insert statement from db1 gets
  transfered into db2 immediately and are waiting for the commit line to
  execute in order to make the insert official?


Transafer is made immediatly, but other sessions will not see inserted data until commit (depends on transaction isolation level)


  Or, are the rows affected by the insert statement are still inside db1
  and are just waiting to be transfered into db2 once the "commit"
  command executes?


Data are not waiting for commit, just storage engine wait for commit to make changes persisted. 


  I wanted to know this facts because I will be copying data from a
  database through the internet and I'm worried that I might lose some
  data in the process... Any help would be greaty appreciated.


Every TCP based connection guarantee data integrity.

You should use replication instead of this approach