I'm assuming you are wanting to do something on the lines of
#!/bin/sh
flag=$(
ssh -T "$user@$host" <<'END_SCRIPT'
# some operations on
# the remote host goes here
# later...
if some_condition; then
echo true
else
echo false
fi
END_SCRIPT
)
This would execute the script given as a quoted here-document on the remote host, and its output would be assigned to the local variable flag. The way I have written it here, I'm assuming that the some operations bit does not produce any output, and instead that the only output produced by the remote script comes from either one of the two conditional echo commands.
Your flag variable would be assigned the string true or false depending on the outcome of some_condition.
If you need to use the value $flag inside the remote script, you would have to use an unquoted here-document. Care must then be taken to properly escape any shell expansions that you would want to happen remotely (by escaping the $ of these as \$).