WASHINGTON—A federal judge has ruled against the National Security Agency’s collection of Americans’ phone records—a victory for civil-liberties advocates—but the impact is limited because the program is due for major changes by month’s end.

U.S. District Court Judge Richard Leon on Monday sided with conservative legal activist Larry Klayman, who had requested a court order barring the NSA from collecting the phone records of some of his clients. The clients had sued the NSA over the data collection following revelations from...