WASHINGTON—A federal judge on Monday ruled against the National Security Agency’s collection of phone records, saying the program “almost certainly does violate” the Constitution.

However, the ruling will have little immediate effect and faces a lengthy future of court proceedings.