The rattlesnake's most typical behavior is to lie motionless, using its coloration to conceal itself, until potential threats pass, or potential food items come within striking distance. Once discovered by a predator, the rattlesnake would clearly benefit by being brightly colored like the coral snake, but such coloration would make it hard for the snake to catch its food. Natural selection has provided an answer in the rattle. The rattle is a remarkable structure that serves as warning badge that uses auditory signals rather than visual signals. The sound that the rattle makes is every bit as recognizable to predators as the coloration of the coral snake.
Predators learn to associate the rattlesnake's buzz with danger.
The fact that the rattlesnake can turn the sound on and off at will means that it can stay concealed when necessary, or if required, raise a warning ruckus. Now, more directly to your question, the rattlesnake doesn't need to hear to know when to rattle. Typically, a rattlesnake confronted by a kingsnake does not use its rattle to warn off the kingsnake. Instead, the rattlesnake has a whole separate set of behaviors that are only exhibited when confronted by a kingsnake. This point is related to the second part of your question.