BOSTON (AP) _ A Superior Court judge ruling in a lawsuit brought by homeless families on Monday ordered the state to immediately raise welfare benefits by nearly 30 percent, a move that could cost more than $400 million.

Gov. Michael Dukakis said he shared the court's sympathy for poor families, but he said the state would appeal the ruling on the grounds that all decisions on spending public funds must be made by the Legislature.

''We feel confident that it will be upheld,'' countered Katherine Mainzer, director of the Massachusetts Coalition for the Homeless, which joined in the suit. ''Today, homeless welfare families are celebrating.''

Judge Charles M. Grabau ordered Welfare Commissioner Charles Atkins to implement a new scale of assistance that would boost benefits for a family of three from the current $476 a month to as much as $926 a month.

The judge also ordered the administration within 30 days to stop placing homeless welfare families in emergency shelters, hotels and motels for periods of more than 90 days. He said ''catastrophic consequences to dependent children living in these temporary shelters require immediate action.''

Grabau's order was a sweeping legal victory for three homeless women who filed suit against the Dukakis administration last June, as well as the 83,000 other families now receiving Aid to Families with Dependent Children, the principal welfare program.

''This is just terrific. This is really a great day for welfare families in Massachusetts,'' said Dottie Stevens, a welfare recipient and director of the Coalition for Basic Human Needs.

But it was unclear immediately how Dukakis could increase benefits without an appropriation by the Legislature to provide the state's 50 percent share of the cost.

Under an earlier order by Grabau, the Welfare Department calculated that AFDC families living in private housing in greater Boston need $11,117 a year. Those living in private housing outside the Boston area need $10,373 a year, according to the government, while families living in subsidized public housing need $7,745.

Current grants for a family of three are $7,176 annually in public housing and $7,884 in private housing.

''As this court's order has been met with indifference or misunderstanding, this court is compelled to spell out more explicitly the steps to be taken in order to achieve obedience to the judicial mandate,'' Grabau wrote.

Dukakis refused Monday to provide specifics from his forthcoming budget message for fiscal 1988, which is to be released publicly Jan. 28. But he said he would address the issues of poverty and homelessness in detail through his inaugural message on Thursday.

He said he believes that homelessness is the result of a housing shortage and that his administration prefers to devote its limited resources to increasing the housing stock.

Grabau in his ruling criticized the executive branch for not acting more swiftly to provide a level of public assistance which the Welfare Department itself acknowledges is necessary to prevent homelessness.

Last year, Grabau ruled that Atkins was violating a 1913 state law that requires the commissioner to calculate a standard of assistance generous enough to allow welfare recipients to raise their children in their own homes.

At that time, he ordered Atkins to calculate such a standard, and the Welfare Department complied by conducting the study. The findings, released in August, stated that welfare recipients should be getting higher benefits, scaled according to circumstances.