We've got lots of things going on here. Let's try to break them down.
Let's start with sed
1) When used with an address or address range, it means "not".
sed '10,/foo/!d infile
This will delete all lines except for the section starting with line 10 and ending with the next line that contains "foo".
2) It can be used as a delimiter in the 's
' substitution, as mentioned. But any simple ascii character can be used, not just '!'. The command will simply use the first character after 's' as the delimiter.
These are all the same:
sed 's/foo/bar/' infile
sed 's!foo!bar!' infile
sed 's_foo_bar_' infile
sed 's@foo@bar@' infile
The address range regex field can be changed too, but you have to precede the delimiter with a backslash.
sed '10,\!foo!!d infile
Now, in the shell...
Among other uses, adding a '!' in front of a command reverses the exit code. A successful 0 exit code will become 1, and a non-zero exit code becomes 0.
ls non-existing-file ; echo $?
! ls non-existing-file ; echo $?
So naturally, when used in a loop or if
test, it means that the sub-command runs if the command fails.