You start with a wedge of $2g$ oriented circles, labeled by $a_i,b_i$. Then each letter in the word corresponds to gluing part of the boundary of the 2-cell to that $1$-cell, in the orientation prescribed by whether you have the letter or its inverse. You get the complete word by travelling around the boundary of the $2$-cell.
Also, look at the picture on page 5 of Hatcher. This explains the construction in detail. For example, the genus 3 surface pictured there has word $[a,b][c,d][e,f]$ as you travel around the boundary of the $2$-cell.
Edit: Here is an actual formua in the case of the torus. We have two circles identified at a point. Let them be parameterized by $\theta_1$ and $\theta_2$ respectively where $\theta_1,\theta_2\colon[0,2\pi]\to S^1$, with basepoint $\theta_1(0)=\theta_2(0)$. Okay, now think of the $2$-cell as a unit square $[0,1]\times[0,1]$. The function from the boundary of the square to the wedge of two circles is given by
$$(0,y)\mapsto \theta_2(2\pi y),\,\,\,\, (1,y)\mapsto \theta_2(2\pi y)$$
$$ (x,0)\mapsto\theta_1(2\pi x),\,\,\,\,\, (x,1)\mapsto\theta_1(2\pi x)$$