The nation's trade deficit, which had ballooned in July, narrowed substantially in August, the Department of Commerce reported yesterday.

The agency said that exports rose to a record $12.5 billion while imports fell 4.7 percent to $14.1 billion. The deficit was $1.6 billion, compared with a July deficit of nearly $3 billion and a June imbalance of $1.6 billion.

Secretary of Commerce Juanita Kreps said in a statement that "the U.S. foreign trade picture has bacone decidedly more favorable since last spring. The trade deficit for the most recent four months is one-third less than that for the first four months of this year. The reduced deficit in August helps to confirm this improving trend."

The dollar gained in trading on news of the improvement in the trade deficit.

In part because the United States has been buying more goods from abroad than it has sold overseas, unwanted dollars have been building up in foreign bank accounts. That has made th value of the dollar decline, especially in relation to the West German mark, the Japanese yen and the Swiss franc.

In the short run, the dollar decline has made imports more expensive, perhaps adding as much as a percentage point to domestic inflation this year. But U.S. officials have hoped that the falling dollar would discourage U.S. consumers from buying foreign goods and make U.S. exports more attractive to foreigners.

Kreps said yesterday that this has been occurring and said the continuing advances in exports are a "fundamental development and not due to special factors related only to thr farm sector or other raw material exports."

Last month, however, agricultural produsts accounted for about half the $700 million increase in exports. Soybean exports alone jumped $142 million in August, while substantial increases were also recorded in cotton, wheat and rice.

The agency also reported a $100 million increase in aircraft exports.

There was decreases in imports across the board, the agency said. A large increase in imports of foreign cars in July was reversed in August.

Foreign car dealers apparently made big puchases of Japanese and German autos in July in an attempt to head off large price increases caused by the declining dollar.

Imports of steel, which had increased sharply in July, rose again in August, according to figures released by the American Iron and Steel Institute, the industry trade group. The government has set up a special program of minimum prices for steel imports to protect the domestic industry.

According to the steel institute, based on figures provided by the Commerce Department, steel imports rose from about 1.78 million tons in July to 1.87 million tons in August - well above the levels recorded in may and June, the first two months the so-called trigger price program was in effect.

But the Commerce Department's official data, which is adjusted for normal seasonal variation, showed a decline in steel imports in August.

In each of the last three years, domestic purchases of foreign-made steel have risen, apparently as auto and appliance makers, among others, get ready for heavy new model production in September.

Last month, because steel exports increased less than anticipated from past experience, the Commerce Department showed a decline in total steel imports from about $434 million in July to $394 million in August.

For the year so far, the nation has imported about $21 billion more than it has exported. For the first seven months of 1977, the deficit was $16 billion.

Last year the nation's trade deficit was $26.6 billion, a record. Officials now expect this year's deficit to be about $28 billion or more.

However, officials also expect the deficit to shrink next year.