Simple theory: K-nearest neighbors predicts a new point by looking at the closest stored examples. For classification, the nearest labels vote; for regression, their values are averaged.
You move to a new neighborhood and want to know what type of area it is. You look at your 5 nearest neighbors — 4 are doctors, 1 is a teacher. You'd guess it's a doctor neighborhood. KNN works identically.
How to measure 'nearest'?
Usually Euclidean distance — the straight line between two points in space. In 3D you can visualize it as the actual physical distance between two floating dots.