How to calculate ray In ray-tracing technique critical point is to calculate rays which came out from eye $E$ to target $T$ through pixel $P_{ij}$ on viewport. The "viewport" is represented as rectangle divided to square pixels - this rectangle is perpendicular to line which go through points $E$, $C$ (viewport center) and $T$. The ray (red line on image) is represented by point $E$ and unit  vector $r_{ij}$ (not shown in picture but it lay on red line) - below is picture which show "geometry" - but what are the formulas to calculate $r_{ij}$? 

The given input values are: 


eye position $E$, 
target position $T$, 
field of view $\theta$ (angle, for human eye $\approx 90^\circ$), 
number of square pixels $k$ (horizontal direction) and $m$ (vertical direction).
$i,j$ processed pixel indexes where $1\leq i\leq k$ and $1\leq j\leq m$   
we also know vertical $w$ vector usually equal to $w=[wx,wy,wz]=[0,1,0]$ (not shown on picture) which indicate where is up and where is down 


The orthogonal vectors $v$ and $b$  (and $t$) on picture are determined by $w$ and $t=T-E$ and maybe will useful in $r_{ij}$ calculations. The $d$ and pixel size is arbitrary and don't change the result because of fixed $\theta$. 

Question: How to calculate unit vector $r_{ij}$ knowing input values described above?
Read up on the basics for ray-tracing here,
Usually we don't mess up with viewports and stuff in raytracing, So I'm just telling you for the case where viewport equals the Image Width and Height.
There are two cases when the field of view changes. Either you move the image plane back and forth or you increase the size. We choose to change $d$ ( former approach).
Calculating ray is simple when the camera is aligned with the world, all you have to do is
$d = 1/tan(\theta_{fov}/2))$
$P_x = P_x + 0.5$
$P_y = P_y + 0.5$
$aspect\_ratio = width/height$
$ray.dir.x = aspect\_ratio *(2*P_x/width) -1$
$ray.dir.y = (2*P_y/height) - 1$
$ray.dir.z = d$
The $Z$ coordinate should be negative or positive depending on whether you are using a right handed or left handed coordinate system. $P_x$ and $P_y$ are pixel coordinates offset by $0.5$ to get the center of the pixel.
The process is simple. You first map your image to NDC space (-1, 1). However to account for non-square images we scale the $X$ coordinate by Aspect ratio so it's $[-a, a]$ in $X$ and $[-1,1]$ in Y. The ray's direction through the pixel is then given by above formulas. You might have to flip the $Y$ coordinate if the image coordinate's origin is top left instead of bottom left.
Note however the link bases all their coordinate systems and space names on RenderMan which is quite different from OpenGL. For camera that isn't aligned with the world coordinate you have to multiply by the transformation matrix as described by bernie in his answer
EDIT:-
First of all let's clear up on -ve and +ve $d$. For right handed coordinate systems you always set $ray.dir.z = -d$ and for left handed $ray.dir.z = d$ This is because in camera space there is no concept of half rays having -ve Z and half +ve, that happens in world space. When you multiply by the transformation matrix it takes care of these cases for you since the transformation matrix is designed keeping in mind whether you are working on a left handed or right handed system. I know i said that you always set it to +ve $d$ but i was wrong in the comments, sorry.
Now coming to your other question. You need the vectors $b,v,t$ and $E$ for creating a transformation matrix. Usually in raytracing you create your camera and initialize its transformation matrix to the standard case (aligned with world space) Then when you move your mouse or something you create a respective rotation matrix and transform the original camera matrix to keep up with these changes. Then when calculating the ray you multiply it as I said above to get ray direction easily. So you probably need to manage your own Camera first.
Since you aren't managing in this case you explicitly need to find these vectors. You mention you have the $w$ vector which is global up. $t$ is easy to find as you say
$t = T-E$
However remember this is the viewing direction not the basis vector. For right handed coordinate systems, the basis vector for $Z$ is $-V_{dir}$
For $b$ which is Camera's right vector. You do,
$ w \times t = b$
This gets you the Camera's up vector. Then you do,
$ t \times b = v$
to get the Camera's up vector. This works as long as $t$ doesn't equal $w$ which is the global up.
So after you have these you can create a camera transformation matrix with $c_1, c_2, c_3, c_4 = b, v, t, E$ respectively.
EDIT2:-
Instead of following above approach you can also do Gram-Schmidt orthogonalization which is more standard in this case. Using this you get the Camera's up vector $v$ as
$v = w - (t.w)\hat{t}$
Then side vector as,
$b = v \times t$
For right handed systems. For left handed systems you need to change the ordering of cross products.