The unrestricted line, restricted line, staff - here's a broad description.
Unrestricted line officer communities are those warfare communities such as surface warfare, aviation warfare, submarine warfare, and a few others, which hold the majority of officers. Officers in those warfare specialties may command at sea and on shore, and not necessarily in their community. Hence, unrestricted. An aviator could command a recruiting district. Having command is critical for promotion for a URL officer. URL officers command the Fleets, become Chief of Naval Operations or Chair of the Joint Chiefs of Staff.
Restricted line officers serve in a more narrowly defined area, such as Oceanography, and will typically spend most of their careers in that area. Their CO roles generally only occur in their specialty. Hence, restricted. Command is not as necessary to their promotion path, but they will serve in equivalent positions of major responsibility. An Engineering Duty Officer (EDO, not EOD) might be a project manager for a new ship.
Staff officers are not line officers, and include JAG, Medical Corps, Civil Engineering (CEC), Supply and others. They will typically serve in those positions their entire career, with some command opportunity.
As always, strong performance in positions of significant responsibility is the key to promotion in ALL communities.
The Naval Academy is designed to produce URL warfare officers. Midshipmen found NPQ for a URL warfare community may be offered restricted line or staff, based on the needs of the Navy at that time and the nature of the NPQ. There are also some limited opportunities for Med Corps and a few others. There are always exceptions and adjustments to policy - what is true this year, may not be in 5. Diligent research of USNA.edu, and briefings while at USNA on available career paths, provide the up-to-date info on available options. There are some paths which allow a USNA grad to go SWO with a (specific community X) option, in which they complete their SWO qualification, and with acceptable performance record, can automatically transfer into X at a certain point. Good questions to ask during a USNA CVW or Admissions briefing.
NROTC, similar to USNA, is geared to producing URL officers, with similar exceptions.
The Navy gains many of its staff officers through direct accession programs. Staff officers gain their college and postgrad degrees and apply for a commission, then go through an officer indoc school. Some of these have scholarship programs, such as HPSP.
Many URL officers, after successfully completing their warfare qualifications in their community, then apply later on in their careers to do what's called "lateral transfer and re-designation" into another community. So, a surface warfare officer might apply to transfer into the Intelligence community, and be re-designated with the numeric designator for that community. The needs of the Navy, as always, drive those processes. The losing community must have the staffing levels to release an officer, and the gaining community must have the capacity to add to its end strength at a given rank and year group. The applying officer must be a strong performer with an excellent record.
Reading official navy.com (Navy recruiting website) pages on various officer communities can be helpful. The key is officer community, not enlisted specialty.
Be sure to read the additional links in the box at the top right of the linked article below.
http://www.public.navy.mil/bupers-npc/officer/Detailing/IWC/intelligence/Pages/OfficerAccession.aspx