A more readable version can look like follows:
-filter_complex "scale=iw*min(1\,min(640/iw\,360/ih)):-1"
640/iw is the horizontal scaling factor and 360/ih is the vertical scaling factor
You want to fit the scaled image inside the output box and keep the (storage) aspect ratio. You do this by selecting the smallest scaling factor with the minimum function: min(640/iw, 360/ih)
You want to prevent any upscaling (i.e. a scaling factor > 1.0) so you add another minimum function: min(1, min(640/iw, 360/ih))
Next step is to calculate the output resolution by multiplying the scaling-factor with input-width and input-height:
output-width = iw * min(1, min(640/iw, 360/ih))
output-height = ih * min(1, min(640/iw, 360/ih))
NextLast step is to construct the filter command. There is no need to specify the output-height, you can specify -1 and ffmpeg will keep the aspect ratio by applying the same scaling factor as for the width.