Ok, Let's first work out the notation.
The partition function can be thought of as merely an normalization of some distribution function, as such, we always must sum over all possible configurations.
The lattice in the above example is comprised of ##N## sites, each has the value ##\pm 1##. I t is convenient to define a set ##\sigma## that has ##N## elements corresponding with eace site. Note that there are ##2^N## different sets that can described a physical configuration,
Our Hamiltonian is a "functional" of the "field" ##\sigma##, by that I mean that the functional associates an energy value for each configuration. More formally, the Hamiltonian is a map from the space of all possible configuration to the space of all possible energy levels ##H:\{\sigma\}\to \{E\}##, where I just introduced the notation ##\{\sigma\}## for the space of all possible configuration (which is a set of ##2^N## sets of size ##N##) , and similarly for the space of all possible energy levels.
Now, as mentioned above, in order to get the right partition function we must sum over all possible configurations and thus our partition function is always defined as,
$$Z=\sum_{\{\sigma\}}e^{-\beta H[\sigma]}$$.
The simplest example would be to show the above for the case of two sites,
$$Z_2=\sum_{\{\sigma\}}e^{-\beta H[\sigma]}=e^{-\beta H[\{+,+\}]}+e^{-\beta H[\{-,+\}]}+e^{-\beta H[\{+,-\}]}+e^{-\beta H[\{-,-\}]}$$.
We shall next try to actually calculate the above partition function.
If I understand correctly, we are looking at the site ##\sigma_0## and it's ##q## neighbours.
For a clearer treatment I will define
$$ H_c[\sigma_0,\sigma]=-h \sigma_0-\left(J \sigma_0+h'\right)\sum_{j=1}^q\sigma_i.$$
The partition function will be
$$Z_c=\sum_{\sigma_0=\pm1}\sum_{\{\sigma\}}e^{-\beta H_c[\sigma_0,\sigma]}\equiv\sum_{\sigma_0=\pm1}Z_0[\sigma_0].$$
From the above lines we find,
$$Z_0[\sigma_0]=\sum_{\{\sigma\}}e^{-\beta H_c[\sigma_0,\sigma]}=e^{\beta h \sigma_0}\sum_{\{\sigma\}}\exp\left\{\beta\left(J \sigma_0+h'\right)\sum_{j=1}^q\sigma_i\right\}.$$
Since the exponent of a sum is just the multiplication of exponents, we can rewrite the partition function as,
$$Z_0[\sigma_0]=e^{\beta h \sigma_0}\sum_{\{\sigma\}}\prod_{j=1}^q\exp\left\{\beta\left(J \sigma_0+h'\right)\sigma_i\right\}.$$
Now come the tricky part, since each term in this multiplication is independent of the other terms we can replace the product and the sum in the following way,
$$Z_0[\sigma_0]=e^{\beta h \sigma_0}\prod_{j=1}^q\sum_{\sigma_j=\pm 1}\exp\left\{\beta\left(J \sigma_0+h'\right)\sigma_i\right\}=e^{\beta h \sigma_0}\prod_{j=1}^q2\cosh\left\{\beta\left(J \sigma_0+h'\right)\right\}.$$
We see now that we are multiplying ##q## identical terms, and so we get the nice (almost) final answer,
$$Z_0[\sigma_0]=e^{\beta h \sigma_0}\cosh^q\left[\beta\left(J \sigma_0+h'\right)\right].$$
Now we just need to sum over ##\sigma_0=\pm1## to find that,
$$Z_c=e^{\beta h}\cosh^q\left[\beta\left(J+h'\right)\right]+e^{-\beta h}\cosh^q\left[\beta\left(J-h'\right)\right]$$